Working with List Items (Client OM)
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.
- ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
- Web oWebsite = clientContext.Web;
- ListCollection collList = oWebsite.Lists;
-
- List oList = collList.GetByTitle("My List");
-
- CamlQuery camlQuery = new CamlQuery();
- camlQuery.ViewXml = "<View><Query><Where><Leq>" +
- "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +
- "</Leq></Where></Query><RowLimit>50</RowLimit></View>";
-
- ListItemCollection collListItem = oList.GetItems(camlQuery);
-
- clientContext.Load(collListItem,
- items => items.IncludeWithDefaultProperties(
- item=>item.DisplayName));
-
- clientContext.ExecuteQuery();
-
- foreach (ListItem oListItem in collListItem)
- {
- Console.WriteLine("ID: {0} Display name: {1}", oListItem.Id, oListItem.DisplayName);
- }
-
How to create the List item
Description: Create new list item in SharePoint list using client Object model...
- ClientContext clientContext = new ClientContext(SPContext.Current.Web.Url);
-
- Web oWebsite = clientContext.Web;
- List oList = clientContext.Web.Lists.GetByTitle("MyList");
-
- ListItemCreationInformation iteminfo = new ListItemCreationInformation();
-
- Microsoft.SharePoint.Client.ListItem listitem = oList.AddItem(iteminfo);
-
- listitem.ParseAndSetFieldValue("Title", "Item added from client OM");
-
- listitem.Update();
-
- clientContext.ExecuteQuery();
-
After execution of above lines, a list item is created in MyList.
How to update list items
Description: Get the ListItem by Item ID and update its title property...
Working with List (Client OM)
Creating List:
Description: ListCreationInformation is used to create sharePoint List in client Object Model… (As ListItemCreationInformation to create List Item)
- string siteUrl = "http://MyServer/sites/MySiteCollection";
-
- ClientContext clientContext = new ClientContext(siteUrl);
- Web oWebsite = clientContext.Web;
-
- ListCreationInformation listCreationInfo = new ListCreationInformation();
- listCreationInfo.Title = "My Announcements List";
- listCreationInfo.TemplateType = (int)ListTemplateType.Announcements;
-
- List oList = oWebsite.Lists.Add(listCreationInfo);
-
- clientContext.ExecuteQuery();
-
Updating a list:
Description: Updates specific property of the SharePoint list..
- ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
- Web oWebsite = clientContext.Web;
- ListCollection collList = oWebsite.Lists;
-
- List oList = collList.GetByTitle("My List");
-
- oList.Description = "Changed description...";
-
- oList.Update();
-
- clientContext.ExecuteQuery();
-
Deleting a list
Description: first select specific list by title then delete that list..
- string siteUrl = "http://MyServer/sites/MySiteCollection";
-
- ClientContext clientContext = new ClientContext(siteUrl);
- Web oWebsite = clientContext.Web;
-
- List oList = oWebsite.Lists.GetByTitle("My Announcements List");
-
- oList.DeleteObject();
-
- clientContext.ExecuteQuery();
-
Adding a field to a list
Description: Adding column to sharepoint list using CAML syntax and AddFieldAsXm()l method..
Working With Website (Client OM)
Creating a Web site
The following example creates a new blog Web site within a site collection.
- string siteUrl = "http://MyServer/sites/MySiteCollection";
- string blogDescription = "A new blog Web site.";
- int blogLanguage = 1033;
- string blogTitle = "Blog Web Site";
- string blogUrl = "blogwebsite";
- bool blogPermissions = false;
- string webTemplate = "BLOG#0";
-
- ClientContext clientContext = new ClientContext(siteUrl);
- Web oWebsite = clientContext.Web;
-
- WebCreationInformation webCreateInfo = new WebCreationInformation();
- webCreateInfo.Description = blogDescription;
- webCreateInfo.Language = blogLanguage;
- webCreateInfo.Title = blogTitle;
- webCreateInfo.Url = blogUrl;
- webCreateInfo.UseSamePermissionsAsParentSite = blogPermissions;
- webCreateInfo.WebTemplate = webTemplate;
-
- Web oNewWebsite = oWebsite.Webs.Add(webCreateInfo);
-
- clientContext.Load(
- oNewWebsite,
- website => website.ServerRelativeUrl,
- website => website.Created);
-
- clientContext.ExecuteQuery();
-
- Console.WriteLine("Server-relative Url: {0} Created: {1}", oNewWebsite.ServerRelativeUrl, oNewWebsite.Created);
-
Retrieving only specified properties of a Web site
- string siteUrl = "http://MyServer/sites/MySiteCollection";
-
- ClientContext clientContext = new ClientContext(siteUrl);
- Web oWebsite = clientContext.Web;
-
- clientContext.Load(
- oWebsite,
- website => website.Title,
- website => website.Created);
-
- clientContext.ExecuteQuery();
-
Updating the title and description of a Web site
- string siteUrl = "http://MyServer/sites/MySiteCollection";
-
- ClientContext clientContext = new ClientContext(siteUrl);
- Web oWebsite = context.Web;
-
- oWebsite.Title = "Updated Web Site";
- oWebsite.Description = "This is an updated Web site.";
-
- oWebsite.Update();
-
- clientContext.ExecuteQuery();
-
Bookmark, share this page if you like, can be handy if you want to look at these operation at one glance.
Thanks
No comments:
Post a Comment