How To: Create a Farm Based and Sandbox Based Solution Web Part using LINQ to SharePoint via SPMetal Utility
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:
- Create a Sand Box Solution with Visual Studio 2010 Beta 2 on SharePoint 2010 Beta
- 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
- Create a Web Part to display information from SharePoint Lists
- 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
- Fire up Visual Studio
- Select SharePoint 2010
- Select Empty SharePoint Project (as you can see I named mine TouchPeopleinSharePoint)
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?
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.
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.
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.
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…
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…
So now for the real work… finally!
- So first I create a List Box object, I will add my result set to that collection (line 16)
- 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)
- I iterate though a list and return the First Name and Last Name via the Add Method of the ListBox Object (line 26 – 33)
That is it! Easy huh… now we just Deploy our Solution…
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..
All being well you should see your solution as I see mine…
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.
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..
Below Same Code… in the Create ChildControls in a NON SandBoxed Solution
U can see in the Properties Window that this one is same code but not SandBox Solution… and it yields results as above…
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..
Paul Swider pointed me to Jie Li’s site where i saw below,
I placed that script in a file and ran it with PowerShell and I got my Web Part to work as indicated below.
We are in business folks…
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.