Search This Blog

Tuesday, February 10, 2015

SharePoint 2010 - Working with ListItem, List, Site - Add, Update, Delete (Client Object Model)


In this section we will see how to Insert, Update and Delete the list items and how it differs from our MOSS2007/2010 server object model.
How to get list items
In this example, you can see,
  • We first create CAML Query object of client object model.
  • Write CAML query and assign to view xml property.
  • Get list items from My list by passing CAML object to getItems method.
  • In load method write what properties you need for those list items in that listitemcollection object.
  • Please note that we don’t have items yet.
  • Finally, Call clientContext.ExecuteQuery that will get list items from server with specified properties.
  • Iterate through that collection object and read list items.
1.          ClientContext clientContext =  new ClientContext("http://MyServer/sites/MySiteCollection");  
2.  Web oWebsite = clientContext.Web;  
3.  ListCollection collList = oWebsite.Lists;  
4.    
5.  List oList = collList.GetByTitle("My List");  
6.    
7.  CamlQuery camlQuery = new CamlQuery();  
8.  camlQuery.ViewXml = "<View><Query><Where><Leq>" +  
9.      "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +  
10.     "</Leq></Where></Query><RowLimit>50</RowLimit></View>";  
11.   
12. ListItemCollection collListItem = oList.GetItems(camlQuery);  
13.   
14. clientContext.Load(collListItem,  
15.     items => items.IncludeWithDefaultProperties(  
16.     item=>item.DisplayName));  
17.   
18. clientContext.ExecuteQuery();  
19.   
20. foreach (ListItem oListItem in collListItem)  
21. {  
22.     Console.WriteLine("ID: {0} Display name: {1}", oListItem.Id, oListItem.DisplayName);  
23. }  
24.       
How to create the List item
Description: Create new list item in SharePoint list using client Object model...
1.          ClientContext clientContext = new ClientContext(SPContext.Current.Web.Url);  
2.    
3.  Web oWebsite = clientContext.Web;  
4.  List oList = clientContext.Web.Lists.GetByTitle("MyList");  
5.    
6.  ListItemCreationInformation iteminfo = new ListItemCreationInformation();  
7.    
8.  Microsoft.SharePoint.Client.ListItem listitem = oList.AddItem(iteminfo);  
9.    
10. listitem.ParseAndSetFieldValue("Title""Item added from client OM");  
11.   
12. listitem.Update();  
13.   
14. clientContext.ExecuteQuery();  
15.       
After execution of above lines, a list item is created in MyList.
http://a2zdotnet.com/Articleimages/525_image1.jpeg
How to update list items
Description: Get the ListItem by Item ID and update its title property...
Creating List:
Description: ListCreationInformation is used to create sharePoint List in client Object Model… (As ListItemCreationInformation to create List Item)
1.          string siteUrl = "http://MyServer/sites/MySiteCollection";  
2.    
3.  ClientContext clientContext = new ClientContext(siteUrl);  
4.  Web oWebsite = clientContext.Web;  
5.    
6.  ListCreationInformation listCreationInfo = new ListCreationInformation();  
7.  listCreationInfo.Title = "My Announcements List";  
8.  listCreationInfo.TemplateType = (int)ListTemplateType.Announcements;  
9.    
10. List oList = oWebsite.Lists.Add(listCreationInfo);  
11.   
12. clientContext.ExecuteQuery();  
13.       
Updating a list:
Description: Updates specific property of the SharePoint list..
1.          ClientContext clientContext =  new ClientContext("http://MyServer/sites/MySiteCollection");  
2.  Web oWebsite = clientContext.Web;  
3.  ListCollection collList = oWebsite.Lists;  
4.    
5.  List oList = collList.GetByTitle("My List");  
6.    
7.  oList.Description = "Changed description...";  
8.    
9.  oList.Update();  
10.   
11. clientContext.ExecuteQuery();  
12.       
Deleting a list
Description: first select specific list by title then delete that list..
1.          string siteUrl = "http://MyServer/sites/MySiteCollection";  
2.    
3.  ClientContext clientContext = new ClientContext(siteUrl);  
4.  Web oWebsite = clientContext.Web;  
5.    
6.  List oList = oWebsite.Lists.GetByTitle("My Announcements List");  
7.    
8.  oList.DeleteObject();  
9.    
10. clientContext.ExecuteQuery();  
11.       
Adding a field to a list
Description: Adding column to sharepoint list using CAML syntax and AddFieldAsXm()l method..
Creating a Web site
The following example creates a new blog Web site within a site collection.
1.          string siteUrl = "http://MyServer/sites/MySiteCollection";  
2.  string blogDescription = "A new blog Web site.";  
3.  int blogLanguage = 1033;  
4.  string blogTitle = "Blog Web Site";  
5.  string blogUrl = "blogwebsite";  
6.  bool blogPermissions = false;  
7.  string webTemplate = "BLOG#0";  
8.    
9.  ClientContext clientContext = new ClientContext(siteUrl);  
10. Web oWebsite = clientContext.Web;  
11.   
12. WebCreationInformation webCreateInfo = new WebCreationInformation();  
13. webCreateInfo.Description = blogDescription;  
14. webCreateInfo.Language = blogLanguage;  
15. webCreateInfo.Title = blogTitle;  
16. webCreateInfo.Url = blogUrl;  
17. webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions;  
18. webCreateInfo.WebTemplate = webTemplate;  
19.   
20. Web oNewWebsite = oWebsite.Webs.Add(webCreateInfo);  
21.   
22. clientContext.Load(  
23.                 oNewWebsite,  
24.                 website => website.ServerRelativeUrl,  
25.                 website => website.Created);  
26.   
27. clientContext.ExecuteQuery();  
28.   
29. Console.WriteLine("Server-relative Url: {0} Created: {1}", oNewWebsite.ServerRelativeUrl, oNewWebsite.Created);  
30.       
Retrieving only specified properties of a Web site
1.          string siteUrl = "http://MyServer/sites/MySiteCollection";  
2.    
3.  ClientContext clientContext = new ClientContext(siteUrl);  
4.  Web oWebsite = clientContext.Web;  
5.    
6.  clientContext.Load(  
7.                  oWebsite,  
8.                  website => website.Title,  
9.                  website => website.Created);  
10.   
11. clientContext.ExecuteQuery();  
12.       
Updating the title and description of a Web site
1.          string siteUrl = "http://MyServer/sites/MySiteCollection";  
2.    
3.  ClientContext clientContext = new ClientContext(siteUrl);  
4.  Web oWebsite = context.Web;  
5.    
6.  oWebsite.Title = "Updated Web Site";  
7.  oWebsite.Description = "This is an updated Web site.";  
8.    
9.  oWebsite.Update();  
10.   
11. clientContext.ExecuteQuery();  
12.       
Bookmark, share this page if you like, can be handy if you want to look at these operation a



No comments:

Post a Comment