Tuesday 13 December 2011

Sharepoint Client Object Model Beginners Guide.


Recently I have been playing around with Sharepoint 2010 Client Object Model. To get started you will need to access and reference the client dll. They are
Microsoft.SharePoint.Client                                       
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime                    
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll

Then to access the library
using Microsoft.SharePoint.Client;

When using this library to access Sharepoint you first create a ClientContext. Then you use the client context to load the queries. Once you are done doing this you then call the method ExecuteQuery.
Here is an example.

ClientContext clientContext = new ClientContext("http://intranet.contoso.com");
Web website = clientContext.Web;
WebCollection websiteCollections = clientContext.Web.Webs;
           
clientContext.Load(website);
clientContext.Load(websiteCollections);

clientContext.ExecuteQuery();
Console.WriteLine("Title: {0}", website.Title);

To recursive list through each folder we can do the following:

foreach (Web websubsites in websiteCollections)
{
Console.WriteLine("Web Sub Sites Title: {0} {1}", websubsites.Title, websubsites.ServerRelativeUrl);
FolderCollection websubsitesfolderCollection = websubsites.Folders;
clientContext.Load(websubsitesfolderCollection);
clientContext.ExecuteQuery();

webSubFolder(clientContext, websubsitesfolderCollection);
}

public static void webSubFolder(ClientContext clientContext, FolderCollection websubsitesfolderCollection)
{
foreach (Folder websubsitefolder in websubsitesfolderCollection)
{
Console.WriteLine("Web Sub Sites Folder:{0} Url:{1}", websubsitefolder.Name, websubsitefolder.ServerRelativeUrl);

FolderCollection websubsitesubfolderCollection = websubsitefolder.Folders;
clientContext.Load(websubsitesubfolderCollection);
clientContext.ExecuteQuery();
webSubFolder(clientContext, websubsitesubfolderCollection);
}
}

To upload a file to the Sharepoint Document Library we can use the code outlined below. Initially we will need FileCreationInformation. Then we attach the file by webSubsiteFolder.Files.Add

if (websubsitefolder.Name == "Shared Documents")
{
FileCreationInformation flciNewFile = new FileCreationInformation();

flciNewFile.Content = WriteSomeData().ToArray();
flciNewFile.Url = "UploadFile.txt";
flciNewFile.Overwrite = true;

File uploadFile = websubsitefolder.Files.Add(flciNewFile);

clientContext.Load(uploadFile);
clientContext.ExecuteQuery();

ListItem fileUploadedItem = uploadFile.ListItemAllFields;

fileUploadedItem["Title"] = "Breeze Document Upload";
fileUploadedItem.Update();

clientContext.ExecuteQuery();
}

public static System.IO.MemoryStream WriteSomeData()
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.TextWriter txtw = new System.IO.StreamWriter(ms);

for (int i = 1;  i <= 20; i++)
{
txtw.WriteLine(string.Format("This is line {0}", i));
}
txtw.Flush();

ms.Seek(0, IO.SeekOrigin.Begin);
return ms;
}

No comments:

Post a Comment