10+ Useful SharePoint Client Object Model Methods
Following the console SharePoint COM app we created previously. Now I will show you how to create other useful methods.
First Open Visual Studio and create an empty C# Console application, name it SP_COMapp.
Add references to Microsoft.SharePoint.Client and Microsoft.SharePoint dlls. (found in Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\ folder)
Your Program.cs should have something like this
Next make sure the reference Copy Local property for these dlls are set to True. (Solution Explorer > Expand References > Click the references > Properties Window > Copy Local > True)
Ok now we're good to go. The namespace region should look like this.
Now our COM methods will be as follows:
List Sites place this inside the Program region not on the main, we'll get back on the main for later ;)
Get Templates
Get List Items
Get List of Contacts using CAML Query
Get List of Tasks
Get List of Events using CAML Query
Get List of Site Groups
Get List of Site Users
Create Site/Page
File Upload
Create Contact Item
Create Task Item
Create Event Item
Create Document Folder
Update Task Item using CAML Query the same rule applies to contacts/events list.
Update Site/Page
Delete Task Item using CAML Query same rule applies to contacts/events list.
Invoke any of these in the main region, but first make sure that all the list you're trying to access are already created.
Alright! That's it for now! Next post I will show you how to do search and using the SharePoint web services for our COM Application. Ka-ciao! ;)
First Open Visual Studio and create an empty C# Console application, name it SP_COMapp.
Add references to Microsoft.SharePoint.Client and Microsoft.SharePoint dlls. (found in Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\ folder)
Your Program.cs should have something like this
using Microsoft.SharePoint.Client; using Microsoft.SharePoint;
Next make sure the reference Copy Local property for these dlls are set to True. (Solution Explorer > Expand References > Click the references > Properties Window > Copy Local > True)
Ok now we're good to go. The namespace region should look like this.
namespace SP_COMapp { class Program { static void Main(string[] args) { } } }
Now our COM methods will be as follows:
List Sites place this inside the Program region not on the main, we'll get back on the main for later ;)
public static void ListSite() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); //security purposes WebCollection web = context.Web.Webs; context.Load(web); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); int i = 0; foreach (Web website in web) { i++; Console.WriteLine(i + "\t" + website.Title + "\t\t" + website.ServerRelativeUrl); } } }
Get Templates
public static void GetSiteTemplates() { using (ClientContext clientContext = new ClientContext("http://sharepointserver")) { clientContext.Credentials = new NetworkCredential("admin", "password"); Web oWebsite = clientContext.Web; uint tempLCID = 1033; Boolean tempBol = true; WebTemplateCollection oTempColl = oWebsite.GetAvailableWebTemplates(tempLCID, tempBol); clientContext.Load(oTempColl); clientContext.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); foreach (WebTemplate oTemplate in oTempColl) { Console.WriteLine(oTemplate.Id + "\t" + oTemplate.Name + "\t\t" + oTemplate.Title); } } }
Get List Items
public static void DisplayList() { using (ClientContext clientContext = new ClientContext("http://devspoint2010")) { clientContext.Credentials = new NetworkCredential("admin", "password"); Web oWebsite = clientContext.Web; List oList = oWebsite.Lists.GetByTitle("Shared Documents"); var files = oList.RootFolder.Files; clientContext.Load(files); clientContext.ExecuteQuery(); String sb = ""; Console.WriteLine("--------------------------------------------------------------- \n"); for (int i = 0; i < files.Count; i++) { sb = sb + files[i].Name + "\n"; } Console.WriteLine(sb); } }
Get List of Contacts using CAML Query
public static void ListContacts() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Contact"); CamlQuery query = new CamlQuery(); query.ViewXml = ""; ListItemCollection listItems = list.GetItems(query); context.Load(listItems); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); foreach (Microsoft.SharePoint.Client.ListItem oitem in listItems) { Console.WriteLine(oitem["Title"] + "\t" + oitem["FirstName"] + "\t" + oitem["Email"]); } } }
Get List of Tasks
public static void ListTasks() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Tasks"); CamlQuery query = new CamlQuery(); query.ViewXml = ""; ListItemCollection listItems = list.GetItems(query); context.Load(listItems); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); foreach (Microsoft.SharePoint.Client.ListItem oitem in listItems) { Console.WriteLine(oitem["Title"] + "\t" + oitem["Status"] + "\t" + oitem["Priority"]); } } }
Get List of Events using CAML Query
public static void ListEvents() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Calendar"); CamlQuery query = new CamlQuery(); query.ViewXml = ""; ListItemCollection listItems = list.GetItems(query); context.Load(listItems); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); int i = 0; foreach (Microsoft.SharePoint.Client.ListItem oitem in listItems) { i++; Console.WriteLine(i + "\t" + oitem["Title"] + "\t" + oitem["Description"]); } } }
Get List of Site Groups
public static void ShowGroups() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web oWeb = context.Web; GroupCollection oGroupColl = oWeb.SiteGroups; context.Load(oGroupColl); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); foreach (Group grp in oGroupColl) { Console.WriteLine(grp.Id + "\t" + grp.Title); } Console.WriteLine("\n"); } }
Get List of Site Users
public static void ShowUsers() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web oWeb = context.Web; CamlQuery query = new CamlQuery(); query.ViewXml = ""; var oUsrColl = oWeb.SiteUserInfoList.GetItems(query); context.Load(oUsrColl); context.ExecuteQuery(); Console.WriteLine("--------------------------------------------------------------- \n"); foreach (Microsoft.SharePoint.Client.ListItem oitem in oUsrColl) { Console.WriteLine(oitem.Id + "\t" + oitem["Title"]); } Console.WriteLine("\n"); }
Create Site/Page
public static void CreateSite() { string blogDescription = "New COM Sample site."; int blogLanguage = 1033; string blogTitle = "Sample Site from COM demo"; string blogUrl = "SP00"; bool blogPermissions = false; string webTemplate = "STS#0"; using (ClientContext context = new ClientContext("http://sharepointserver")) { Web web = context.Web; context.Credentials = new NetworkCredential("admin", "password"); WebCreationInformation webCreateInfo = new WebCreationInformation(); webCreateInfo.Description = blogDescription; webCreateInfo.Language = blogLanguage; webCreateInfo.Title = blogTitle; webCreateInfo.Url = blogUrl; webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions; webCreateInfo.WebTemplate = webTemplate; Web oNewWebsite = web.Webs.Add(webCreateInfo); context.Load( oNewWebsite, website => website.ServerRelativeUrl, website => website.Created); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); } }
File Upload
public static void UploadDocument() { string documentListName; string documentName; byte[] documentStream; documentListName = "Shared Documents"; documentName = "sample.txt"; documentStream = GetBytesFromFile("C:\\sample.txt"); using (ClientContext clientContext = new ClientContext("http://sharepointserver")) { clientContext.Credentials = new NetworkCredential("admin", "password"); List documentsList = clientContext.Web.Lists.GetByTitle(documentListName); var fileCreationInformation = new FileCreationInformation(); fileCreationInformation.Content = documentStream; fileCreationInformation.Overwrite = true; fileCreationInformation.Url = "http://sharepointserver/" + documentListName + "/" + documentName; Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add( fileCreationInformation); uploadFile.ListItemAllFields.Update(); clientContext.ExecuteQuery(); Console.WriteLine("Success!! \n"); } } public static byte[] GetBytesFromFile(string fullFilePath) { FileStream fs = System.IO.File.OpenRead(fullFilePath); try { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); fs.Close(); return bytes; } finally { fs.Close(); } }
Create Contact Item
public static void CreateContact() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Contact"); ListItemCreationInformation newItem = new ListItemCreationInformation(); Microsoft.SharePoint.Client.ListItem listItem = list.AddItem(newItem); listItem["Title"] = "Tester"; listItem["FirstName"] = "Admin"; listItem["Email"] = "admin@sharepointserver"; listItem["Company"] = "Contoso"; listItem.Update(); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); } }
Create Task Item
public static void CreateTask() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Tasks"); ListItemCreationInformation newItem = new ListItemCreationInformation(); Microsoft.SharePoint.Client.ListItem listItem = list.AddItem(newItem); listItem["Title"] = "Item Created through C# Console"; listItem.Update(); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); }
Create Event Item
public static void CreateEvent() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web web = context.Web; List list = web.Lists.GetByTitle("Calendar"); ListItemCreationInformation newItem = new ListItemCreationInformation(); Microsoft.SharePoint.Client.ListItem listItem = list.AddItem(newItem); listItem["Title"] = "New Event Created through C# Console"; listItem["EventDate"] = DateTime.Now; listItem["EndDate"] = DateTime.Now.AddHours(1); listItem["Description"] = "Test Event from C# Console COM app"; listItem.Update(); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); } }
Create Document Folder
public static void CreateFolder() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); Web oWebsite = clientContext.Web; List oList = oWebsite.Lists.GetByTitle("Shared Documents"); if (oList != null) { String folder = "Sample Folder"; var folders = oList.RootFolder.Folders; clientContext.Load(folders); clientContext.ExecuteQuery(); var newFolder = folders.Add(folder); clientContext.ExecuteQuery(); Console.WriteLine("Success!! \n"); } } }
Update Task Item using CAML Query the same rule applies to contacts/events list.
public static void UpdateTask() { using (ClientContext context = new ClientContext("http://sharepointserver")) { context.Credentials = new NetworkCredential("admin", "password"); List list = context.Web.Lists.GetByTitle("Tasks"); CamlQuery query = new CamlQuery(); query.ViewXml = ""; ListItemCollection listItems = list.GetItems(query); context.Load(listItems); context.ExecuteQuery(); Microsoft.SharePoint.Client.ListItem item = listItems[listItems.Count - 1]; item["Status"] = "In Progress"; item.Update(); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); } } New Event Created through C# Console
Update Site/Page
public static void UpdateSite() { using (ClientContext context = new ClientContext("http://sharepointserver/sample00")) //exact url of the site/page { context.Credentials = new NetworkCredential("admin", "password"); Web oWebsite = clientContext.Web; oWebsite.Description = "Updated Site from Console App"; oWebsite.Update(); clientContext.Load(oWebsite); clientContext.ExecuteQuery(); Console.WriteLine("Success!! \n"); } }
Delete Task Item using CAML Query same rule applies to contacts/events list.
public static void DeleteTask() { using (ClientContext context = new ClientContext("http://sharepointserver/sample00")) { context.Credentials = new NetworkCredential("admin", "password"); List list = context.Web.Lists.GetByTitle("Tasks"); CamlQuery query = new CamlQuery(); query.ViewXml = ""; ListItemCollection listItems = list.GetItems(query); context.Load(listItems); context.ExecuteQuery(); listItems[listItems.Count - 1].DeleteObject(); context.ExecuteQuery(); Console.WriteLine("Success!! \n"); } } New Event Created through C# Console
Invoke any of these in the main region, but first make sure that all the list you're trying to access are already created.
static void Main(string[] args) { CreateSite(); }
Alright! That's it for now! Next post I will show you how to do search and using the SharePoint web services for our COM Application. Ka-ciao! ;)
Comments
Post a Comment