In the last post we created the sample for creating the web using various available templates Part -2 .
I will recommend you to go through Part -1 to Part -2 and then continue reading to have a better understanding.
In this post we will create list using managed client object model.
Create List :
To try this sample code , you can create a console application and then create a method CreateList and pass SiteURL where you want to create the list.
using (ClientContext clientContext = new ClientContext(SiteURL)) { List targetList = null; ListCreationInformation NewListInfo = new ListCreationInformation { //Can set the list template here. TemplateType = (int)ListTemplateType.GenericList, Title = "NewList", Url = "NewList", QuickLaunchOption = QuickLaunchOptions.On // to set the quick launch property of the list. }; targetList = clientContext.Web.Lists.Add(NewListInfo); clientContext.Load(clientContext.Web.Lists); clientContext.ExecuteQuery(); } |
Important : For creating the List we need to use the ListCreationInformation object and set the properties.
And finally load the newly created list and call Execute query method of the client context. Until this execute query method is called changes will not be updated on the site. This executeQuery method bundles all the request and executes them on the server. This reduces the server round trip.
This will create the list in the site and this list will be available on the quick launch menu of the site.
Add fields in the List :
Objective is to add fields in the list. In this code snippet we are creating fields of type text,Number,URL, You can create fields with other available types as well .
using (ClientContext clientContext = new ClientContext(SiteURL)) { //To retrieve collection of items from the list based on CAML query. List lst = clientContext.Web.Lists.GetByTitle("NewList"); FieldCollection fields = lst.Fields; //Add column of type Text FieldType fieldType = FieldType.Text; fields.AddFieldAsXml( String.Format("<Field DisplayName='{0}' Type='{1}'></Field>", "Name", fieldType), true, AddFieldOptions.DefaultValue); //Add column of type Number FieldType fieldType1 = FieldType.Number; fields.AddFieldAsXml(String.Format("<Field DisplayName='{0}' Type='{1}'></Field>", "ProductCount", fieldType1), true, AddFieldOptions.DefaultValue); //Add column of type Number FieldType fieldType2 = FieldType.URL; fields.AddFieldAsXml(String.Format("<Field DisplayName='{0}' Type='{1}'></Field>", "SiteLink", fieldType2), true, AddFieldOptions.DefaultValue); lst.Update(); clientContext.ExecuteQuery(); } |
Create List Item :
Our objective is to add items in the newly created list. Right now this list has only one default field Title, so we will add item in the list and set value for that field only.
using (ClientContext clientContext = new ClientContext(SiteURL)) { List lst = clientContext.Web.Lists.GetByTitle("NewList"); ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); ListItem newListItem = lst.AddItem(newItemInfo); newListItem["Title"] = "abc"; newListItem.Update(); clientContext.Load(newListItem); clientContext.ExecuteQuery(); } |
Important : For creating the List we need to use the ListItemCreationInformation object and set the properties.
And finally load the newly created item and call Execute query method of the client context. Until this execute query method is called changes will not be updated on the server. This executeQuery method bundles all the request and executes them on the server. This reduces the server round trip.
Update Item in the List :
Our objective is to update the item in the list. For this first we need to access the list , then we need to access the list item. Every list item has unique auto incrementing integer value which can be used to retrieve the particular item and then update the field of the ListItem.
using (ClientContext clientContext = new ClientContext(SiteURL)) { ListItem lstItem = clientContext.Web.Lists.GetByTitle("NewList").GetItemById(2); lstItem["Title"] = "UpdateTitle"; lstItem.Update(); clientContext.ExecuteQuery(); } |
Important :
<!1) For updating the field of type HyperIink use this code snippet
FieldUrlValue link = new FieldUrlValue(); link.Url = "http://microsoft.com"; link.Description = "World of Microsoft Technologies"; lstItem["URL"] = link; lstItem.Update(); clientContext.ExecuteQuery(); |
<!2) For updating the field of type “Person or Group “ use this code snippet
User user = null; FieldUserValue fldUserValue = new FieldUserValue(); user = clientContext.Web.EnsureUser("domain\\userId"); clientContext.Load(user); clientContext.ExecuteQuery(); fldUserValue.LookupId = user.Id; lstItem["User"] = fldUserValue; lstItem.Update(); clientContext.ExecuteQuery(); |
This way we can update the specific item from the list based on the auto incrementing ID.
Happy Coding J
Continue reading Part -4
No comments:
Post a Comment