Archive

Posts Tagged ‘Sandboxed solutions’

Develop, Deploy and Debug Sandboxed Solutions

December 18th, 2009 4 comments

Sandboxed solution in SharePoint 2010 is a new way of deployment for rapid development. You can deploy your solutions on site collections without resetting the farm. Sandboxed solutions are per site collection. You can create them like the normal farm solutions. You can see the detailed information about sandbox solutions here.

Develop SandBoxed Solution

 In Visual studio 2010 there is a lot of support for SharePoint projects. Whenever you start a new SharePoint project (except a few options) it will ask you about the solution type in second window. By default sandboxed solution is selected as solution type.

Select Deployment type

You can also change the deployment type at any time from the project properties window.

Change Solution Deployment type

In a SharePoint solution you can have assemblies, features, Web Parts, list definitions and feature receivers. Now if you want to add a Web Part in this solution, right click on the solution and select add new item.

Add new item

Select Web Part from the available option. In Sandboxed solutions you cannot use Visual Web Part. Actually from sandboxed solution you can deploy files to web front-end server. Visual web part deploys visual files to server. So you can use the simple Web Part from the option. If you select Visual Web part, you will notice that sandboxed solution option will be grayed-out in next screen, where you define deployment type.

Select Web Part

Now you can start writing your code for web part. I have added a snap-shot of sample code.

Code

It’s a simple piece of code, accessing a list from the site collection and displaying items in a ListBox control. In sandboxed solutions you can access all the classes under SPSite, SPWeb, SPList, and SPListItem. Sandboxed solutions deploy as partial-trust solutions, so there are some limitations. You cannot use classes other than mention above. For example you cannot use code for run with elevated privileges block. If you try to use classes outside the scope of sandboxed solution you won’t get IntelliSense from Visual Studio. If use SPSecurity.RunWithElevatedPrivileges it will compile without any error message. It is because of the fact that visual studio compile against the full API of the SharePoint. Visual studio helps to write code specific for sandboxed solution by providing proper IntelliSense for available classes. But if you use code from a full-trust solution, there is no way to detect error in visual studio. You can only see exception when this code execute in sandboxed process. The exception thrown is “Could not load type ‘CodeToRunElevated’.

Use SPSecurity Error

When your build is successful, you can create a wsp file. In visual studio 2010, create a wsp is very simple. Previously it was another painful task to create wsp file. You had to use different ways to create a cab file and then rename it with extension wsp.  Now you can right click on the solution and select ‘Package’ option. It will create a wsp file for current solution in bin/Debug folder.

Create wsp solution
Deploy Sandboxed solutions
Deployment of sandboxed solution is very simple and rapid. You just have to upload your solution in a library name solutions. Every site collection in SharePoint 2010 has a solutions library. You can see more details about solution library here. Solutions library is a SharePoint list. When you upload your solution it will prompt to activate it.  If you like to upload a solution, better way to open the solution files in visual studio and then after the changes build and create a new solution. Now deactivate old solution from the solutions library and upload new solution. Finally activate it. Your solution is now deployed including the web part. Here you can also see that how you can use your custom web part in SharePoint pages. Click here

Debug Sandboxed solution
We know that SharePoint sites runs under the IIS worker process w3wp.exe. In order to debug SharePoint sites we attach our code to w3wp.exe process. But sandboxed solutions run under a different process Sandboxed worker process (SPUCWorkerProcess.exe).  so if you want to debug your code in sandbox solution you have to attach your code to SPUCWorkerProcess.exe and if you like to debug rest of the page then you have to attach it to w3wp.exe process.

Task manager view

I hope it will be helpful, I will try to explore more in coming days and will share again.

Solutions Library – SharePoint 2010

December 17th, 2009 53 comments

One of the salient features in SharePoint 2010 is sandboxed solution. You can see the detailed introduction to sandboxed solutions in my other blog. Every site collection in SharePoint 2010 has a library with name Solutions. You can upload your sandboxed solutions in this library. This is a simple SharePoint list. In order to access it you have to open site settings page. You can select Site Settings option from site actions menu.
From site settings page, there is a link for Solutions library under Galleries section.Site Settings Page

Solution library displays the list of solutions, their status and the resource usage as well. It also displays resource quota information for server resources, also current usage and average usage. You can also activate and deactivate solutions. In solutions library you can upload your solutions. For this you can select upload solution option from the ribbon.

Solutions Library

When you click on upload solution, it will open a window like this.

Upload Solution

This window is saying upload document, I don’t know why. May be it’s just a beta version. It should say upload solution, and solution file could be pre-selected in file type filter. You can browse your solution (wsp) file. You can activate the solution straight away after the upload or at any other time.

Activate Solution

You can remove any solution from the library. But this solution should be deactivated first. You can also upgrade any solution but the condition is same, it should be deactivated first. In order to upgrade the solution your solution id should be the same. Whenever you upgrade a solution, it will deactivate the old version and activate the new version.  If you try to upgrade any activated solution, you will see error windows like this.

Upload same solution - Error

In solutions library, you can upload any type of file. But the ribbon is context sensitive. Activate, Deactivate and upgrade buttons could be enabled only for wsp files.
I hope it will be informative about solutions library.

Sandboxed Solutions in SharePoint 2010

December 14th, 2009 2 comments

SharePoint 2010 came with lots of new features and ways for developers and users. Sandboxed solution is one new way of deploying solutions. You can also have the solutions as you have in SharePoint 2007, but those solutions will be called farm solutions. Sandboxed solution is per site collection. Sandboxed solution’s deployment is different than normal solutions. Every site collection in SharePoint 2010 has a library for sandbox solutions. You simply need to upload your solution (wsp) to this library. And then activate the solution. So no server reset is required.
Sandboxed solution doesn’t make server reset, so you can do as much deployments as you want. It is very convenient from developer’s point of view. But at the same time there are some limitations for sandboxed solutions. Farm solutions run with full trust, whereas sandbox solutions mark as partially trusted solution. The major difference is the way of deployment. You can run the same solution under full-trust, but then you need to deploy it as a farm solution. As you do in SharePoint 2007.
The limitations for the sandboxed solution is due to the factor that it should not be a performance hinder for farm and application. These are the major limitations I found so far in sandboxed solutions.

  • You cannot run your code with elevated permissions in sandboxed solution
  • You cannot use Visual web part template for sandboxed solution. Because sandbox solution cannot deploy files to web front end.
  • You cannot access internet to make web service calls directly
  • You cannot access hard drive to read/write files
  • you can’t access code that is not marked to allow partially trusted callers
  • you cannot deploy assemblies to GAC

It seems that in sandboxed solutions’ you cannot do much but still you can. In sandbox solution you can access all the classes under SPSite, SPWeb, SPList, and SPListItem. You can create Web Parts, list definitions and instances, content types and fields, modules, declarative workflows, and event receivers. You can read and write to lists and libraries within same site collection. Sandboxed solutions provide enough functionality to build most of the applications. The best thing is rapid deployment and no server reset.
Being a developer you would definitely want to go out of the limited scope of sandboxed solutions, like communicate with a database or web service. According to SharePoint team the best way to reach out of the sandbox is by using the Business Connectivity Services (BCS) to create an external content type. You can then read and write to the data source from the sandboxed solution. Another, more advanced, way to reach outside the sandbox is to create a class that runs in a full-trust process outside the sandboxed worker process to proxy calls. This proxy class is deployed as a farm solution and is callable from the sandboxed solution.
Sandboxed solution runs under a different process, farm solutions run under IIS worker process (w3wp.exe). But sandboxed solution runs under the sandbox worker process SPUCWorkerProcess.exe.
In sandboxed solutions library, you can activate and deactivate solutions. You can also remove solution from the library, but solution should be in deactivated state. You can also upgrade a sandboxed solution. To upgrade a solution, you need to create a .wsp file that has a new file name but the same solution ID. The easiest way is to use Visual Studio 2010 for it. Open the same solution and after the changes, give the package a new name using package designer. When you try to upload new solution, it will detect the solution with same id and ask for upgrade. After you upgrade, you will see it activated new version and deactivated old solution.

Sandboxed solution is a great enhancement for developers and also for administrators. Now all the solutions deployment is not the responsibility of the farm administrators. Site collection administrator can deploy, manage and monitor sandboxed solutions for their site collections. Developers can test functionality without disturbing the whole farm. They can deploy the same solution with full trust later, when they have confidence in the new functionality or at convenient time. Isn’t that a great news.

Cheers