Fabian Williams SharePoint Blog

Solving problems with SharePoint day and night

started to use HootSuite thanks @EUSP Ma

started to use HootSuite thanks @EUSP Mark Miller for the tip

Advertisements

January 17, 2010 Posted by | Uncategorized | Leave a comment

How To: Create a Farm Based and Sandbox Based Solution Web Part using LINQ to SharePoint via SPMetal Utility

Synopsis

In this Blog and in all the blogs i post, I try to re-use my work/efforts; for one, it ensures that the information is grounded, it certainly gives me practice, and more importantly, it demonstrates how SharePoint is modular, and how each part of the Wheel Compliments each other.  So.. </soapbox> over, lets begin… The idea here is to demonstrate how we can affect changes to SharePoint Lists and Libraries; for instance, we will use LINQ to SharePoint to talk to Lists in a Site, and use Sandbox Solutions to "work in that list in a protected area” based on the Rules of the Sandbox.  Now; the intrusive part is the how we gain access to the SharePoint List, in this example (Part 1) I am still doing a Sandbox Solution which means it is limited to the Site Collection, but I still have code that may be better protected in a “Full Trust Proxy” which will be (Part 2).

Get ready on how you can do:

  1. Create a Sand Box Solution with Visual Studio 2010 Beta 2 on SharePoint 2010 Beta
  2. Use LINQ to SharePoint via the SPMetal Utility to create and add a Class to our Solution to act as the Wrapper to our Content Database
  3. Create a Web Part to display information from SharePoint Lists
  4. Use some Code to do some actions on that List

Recognition of Effort to my friends in the SharePoint Community

So… I want to take time out to recognize some of my buddies in the SharePoint Community that helped with a really weird issue I was having trying to get even a “Hello World” textbox control working in my Sandbox Solution.  As it turned out and I do have the troubleshooting steps outlined below, my issue was related to having SharePoint 2010 on a Domain Controller, and the problem was solved [albeit after 12 hours of work and 4 – 6 hours troubleshooting] via a PowerShell Script found on Jie Li’s and Jeremy Thake’s site.  Using Twitter (yes I know, I tweet too much) I solicited the help of my co-worker Bart Tubalinal @bart_tubalinal and Jeremy Thake @jthake to see if they saw something glaring wrong in my code; they looked, they found nothing… then i hear the words I didn’t want to hear “… have you tried looking in the ULS log…, this may be your environment…” this would have not shocked me as much if I didn’t have to deliver at Virginia Beach SharePoint Saturday this coming Saturday (1/9/2009), and didnt want to risk doing an re-install my OS. In the end Paul Swider @pswider asked if i ran the script found on Jie Li’s site; as soon as i placed it in a file and ran it with PowerShell, BOOM! I was good to go!  Mark Miller @EUSP of End User SharePoint pointed out that he may emphasize the community involvement in a Keynote he is prepared to give, I encourage him to do so.

Let us Begin…

 

Part 1: This is Part 1
Part 2: Doing everything but using a Full-Trust Proxy for the most Intrusive Part of the job!

So the first thing we need to do is crack open our spiffy Visual Studio 2010 and Create a new SharePoint Empty Project

clip_image001

  1. Fire up Visual Studio
  2. Select SharePoint 2010
  3. Select Empty SharePoint Project (as you can see I named mine TouchPeopleinSharePoint)

clip_image002

Next you will be prompted to choose if this is a Sandboxed Solution or a Farm Solution, I selected “the Default” which is SandBoxed Solution… so what is Microsoft trying to tell us here folks?

clip_image003

Once you have done that, click Finish to close out the dialog box.  Next just to be sure, you can never be too careful; since I am building against a x64 bit system, I wanted to make sure that my Project Properties Build Targets were appropriately set.

clip_image004

Once I have completed that process, next I am going to add a new Web Part. NB, you are not permitted to do Visual Web Parts in SandBoxed Solution because that would mean that you would need to deploy it on the WFE; which under SandBoxed Solutions, we don’t want to, so… we wanna select a regular web part.

clip_image005

See below…

clip_image006

So once the Web Part is added, we have the following cs file created, clearly you can see we have methods that are over-ridden which we will employ in our solution.

clip_image007

Before we get there however, I need to do some prep work..

  • I need to Add my SPMetal Class
  • I need to make a reference to Microsoft.SharePoint.Linq

This Class that I am adding is created by SPMetal Utility and it basically provides me Entity Classes and a Context Object to manipulate SharePoint Lists via LINQ to SharePoint. I use this because all that plumbing is already done for me and hey, work smarter not harder…

clip_image008

clip_image009

clip_image010

NB. The Red squigglies are there in this CharlieSiteLists.cs file because I dont at this time have a Reference Set for Microsoft.SharePoint.Linq which i will do below…

clip_image011

So now for the real work… finally!

  1. So first I create a List Box object, I will add my result set to that collection (line 16)
  2. I make a reference to the CharlieSiteList.cs (SPMetal Utility Created File) Data Context Object so I can use it as a Wrapper to the Content Database (line 25)
  3. I iterate though a list and return the First Name and Last Name via the Add Method of the ListBox Object (line 26 – 33)

clip_image012

That is it! Easy huh… now we just Deploy our Solution…

clip_image013

Next I go to my site under “Site Pages” and create a Web Part Page…but before I do that, lets just make sure that our Sandboxed Solution is there in the Gallery. We find this by clicking on Site Settings and under Galleries, you will see “Solution”.. go ahead and click that bad bwoy..

clip_image014

All being well you should see your solution as I see mine…

clip_image015

Now, we edit that page we would have created to hold our Web Part or you can just put it in a page of your choosing. 

clip_image016

So, here is where the SharePoint community came alive for me last night.. and although SharePoint threw no Error in the Event Viewer, nor did the Actions yielded any “gotchas” that I did anything wrong, when I added the Web Part to a zone..

When I add the TouchPeopleNOLookupList Web Part to the Left Column Zone as indicated below it goes though the motion but ADDs Nothing but in the ShowMyProfile Web Part it works… that web part is a Farm Level Solution though… same code.. 

clip_image017

Below Same Code… in the Create ChildControls in a NON SandBoxed Solution

clip_image018

U can see in the Properties Window that this one is same code but not SandBox Solution… and it yields results as above…

clip_image019

So as part of my debugging I downloaded this ULS viewer from CodePlex at http://ulsviewer.codeplex.com/ and I saw that, Indeed, I have a few errors going..

image

Paul Swider pointed me to Jie Li’s site where i saw below,

image

I placed that script in a file and ran it with PowerShell and I got my Web Part to work as indicated below. 

image

We are in business folks…

image

Stay tuned for Part 2 where I will do this all with a Full Trust Proxy i.e. move the intrusive Class that actually touches the Content Database to the GAC and use a Proxy to call it.

 

Have fun! 

January 4, 2010 Posted by | SharePoint Administration, SharePoint Bug, SharePoint Development, SharePoint Error/Resolution, SharePoint How-To, Strange Stuff, Visual Studio 2010 | , , , | 3 Comments

How To: Using SPMetal Utility in SharePoint 2010 to “presumably” manipulate data on a BCS External List

Synopsis

So this blog started out with me trying to prove that can use LINQ to SharePoint via the SPMetal Utility in SharePoint 2010 to gain access to Business Connectivity Services (BCS) External Content Types // External Lists. In plain English, I wanted to use LINQ to SharePoint in as little code as possible and without using the BCS Template to do CRUD actions to External Lists created in SharePoint. I found out out that I was unable to using SharePoint 2010 BETA and Visual Studio 2010 Beta 2… basically SPMetal did not create the necessary Entity Classes for External Lists in the resultant C# file.

Therefore, I will turn this blog into a How-To for using the SPMetal Utility and point you to Serge Luca Blog for some great Web Cast on just how to use LINQ to SharePoint to access Lists in SharePoint 2010!

So the first thing you can do is bone up on SPMetal. You can read up on it at http://msdn.microsoft.com/en-us/library/ee538255(office.14).aspx or BING “SPMetal SharePoint”

clip_image001

Next I picked a SharePoint 2010 Site that had few clutter and still had List Created OOB that I could play with.

clip_image002

I settled on my “Charlie” Site

clip_image003

Next, I added an External Content Type to create an External List called “A Few People I Know” which resulted in the two screen shots below…

clip_image004

clip_image005

Then just to make sure I do my due diligence, I created two Custom Lists

  • One List that will do lookups to my external List but remains a custom list
  • One List that will just be a custom list with regular columns

clip_image006

So in my list called “Regular People I know List” I am doing lookups to my External Lists Data…

clip_image007

My Resulting List looks like below…

clip_image009

Then I created my RegularPeopleNOLookup List which is just a vanilla list with stuff in it.

clip_image010

Now Time for the FUN! Stuff, we get to crack open Visual Studio 2010 Beta 2 and Code Away!

Steps I took that led be to below..

  • Created a Console Application
  • Set the Build Type Platform to “Any CPU” since i am on a x64 bit system
  • Add References to
    • Microsoft.SharePoint
    • Microsoft.SharePoint.Linq

clip_image011

Next I will use SPMetal to create located in the “BIN” folder under the “14-Hive”, is that what we are calling it… I cant remember, but I know we were given the nomenclature in Vegas! 🙂

clip_image012

The syntax for the utility against my Charlie Site is below..

clip_image013

At this time now as you can see in my working folder, I have a file created called “CharlieSiteLists.cs” which is where the Entity Classes are located for the Lists in the “Charlie” site… My HOPE is/was that I will get my External List showing up too.. bummer it didn’t…

Below you have the “CharlieSiteListDataContext” which is basically your wrapper to the Content Database through LINQ

clip_image014

clip_image015

 

clip_image016

Below you can see the Entity Classes representative of the SharePoint List in the “Charlie” Site.

clip_image017

 

clip_image018

What helps us out here is that the Entity List Class implements IQueryable and IEnumerable which will help us out in our LINQ operations.

image

So here is where I will bail out…since I wasn’t able to get to my SharePoint –> Business Connectivity Services –> External Content Type –> External List that I created in my Lab. So Just to show you how it LINQ to SharePoint works see below how I add a data element to my List… MUCH better examples and walkthru are available on Serge Luca’s Blog.

clip_image019

clip_image020

Have Fun with That! Hopefully the story changes with External List when RTM comes out… that’d be really cool.

January 3, 2010 Posted by | Business Connectivity Services, SharePoint 2010, SharePoint Development, SharePoint How-To | , , | 5 Comments