Pages

Tuesday, 7 April 2015

From SharePoint On-Premise to Office365


In this post I will show you how you can convert you SharePoint farm solutions (.wsp) to solution that works with Office365/cloud.
Following is the road map from Microsoft that shows how Microsoft is transforming Office product for every platform:

Picture copied from Microsoft Office365 Developer site.
It looks like we have now more audiences (Developers) or we can say that we have more options to develop solutions that target Microsoft Office product.
Following are the common tasks that we usually do when we develop SharePoint Farm solutions:
1.      Site Definition
2.      Site creation
3.      Item Receiver code
4.      Feature (I.e. To create site columns, content types, list or document library etc)
5.      Site Columns and Content Types
6.      List or document library creations
7.      Workflow
8.      File upload using Module
9.      Timer job
10.   Querying sites, lists and other SharePoint objects
11.   Item creations in list
12.   Branding (customising master page, page layouts etc)
13.   WebPart development
To convert Farm solutions code to cloud compatible we have got following choices to host code:
1.      ASP.NET MVC application
2.      ASP.NET Web-Form application
3.      Console Application (for continues integration environment)
4.      Windows Phone/Table client
5.      Php application
6.      Android / iOS application
I will start with Console application that query SharePoint objects from On-Premise SharePoint farm as well as Office365. By doing this way we can prepare ourselves for cloud.
Let’s start writing some code that works in both environment.
bool forCloud = true;

Console.WriteLine("Connecting to Office365 at https://your-tenancy.sharepoint.com...");

// Open connection to Office365 tenant
ClientContext clientContext = new ClientCon-text("https://your-tenancy.sharepoint.com");
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

if (forCloud)
{
   //creating secure string
   SecureString password = new SecureString();
   foreach (char c in Office365Password)
      password.AppendChar(c);

   clientContext.Credentials = new SharePointOnlineCredentials(Office365UserId, password);
}
else
{  
   //Comment this line if you want to use your default network credential
   clientContext.Credentials = new NetworkCredential("UserName", "Password", "Domain");
}

Console.WriteLine("Executing query...");

//load web
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();

//displaying title of the web.
Console.WriteLine("Web title: {0}", web.Title);

Console.WriteLine("Loading lists...");
ListCollection lists = web.Lists;
clientContext.Load(lists);
clientContext.ExecuteQuery();

foreach (List list in lists)                
  Console.WriteLine("List title: {0}", list.Title);                

Console.WriteLine("Done.");

For on premise just set onCloud to false and rest of the code would be same for both environment. After executing it will show web site name and all lists within the site.
Using this simple technique we can develop app-part, which I will blog soon, to show aggregated data from various lists in Office365.
In next post, I will show you how we can leverage ASP.NET MVC application to host SharePoint CSOM code that will be used to create site definition and other stuff.

That's it for now. Please leave your valuable feedback.

Wednesday, 18 March 2015

Deploy Content Type with Lookup field


In this post I am going to show how we can deploy content type declaratively (through xml) with fields using known GUIDs.

One of the challenge with the look up field is that it binds to Lookup list via ListId (i.e. GUID). There are couple of ways you can achieve this. One is to use SharePoint Object Model to create fields and content types via Feature code.

First you need to create the lookup list via site scope feature. You might be thinking why I am creating a list in site scope feature? The reason is simple I just want to create the lookup list in root web of the site collection and that will be accessible to site columns.

Here is the project structure shown in image:




The LookupListProvisioner feature receiver will create a list that can be used as look up field. Here is the code:



Next, you need to create site column using SiteColumnsProvisioner feature receiver using below code:



As you can see in the code, we are using existing field GUID that can be used in Content Type. In ContentTypeProvisioner feature, I have used Elements.xml file to create the content type like shown below:



As you can see in above code, I have used pre-defined ContentTypeId and Field Guid. I have written a tool, that generates xml for fields and content types from SharePoint environment, can be found at http://sharepointxml.codeplex.com/
That’s it for now. Happy coding J