Updating a field value in SharePoint using a client-object model

So, I'm trying to create a method that is essentially used to change the value of a field in SharePoint.

This is what I still have ...

static String fieldName1 = "Title"; static String fieldName2 = "Keywords"; static String title = "A Beautiful Sunset"; static String keywords1 = "test1"; static String keywords2 = "test2"; static String keywords3 = "test3"; static NetworkCredential credentials = new NetworkCredential(username, password, domain); static ClientContext clientContext = new ClientContext(URL); static Web site = clientContext.Web; static List list = site.Lists.GetByTitle(listName); static FileCreationInformation newFile = new FileCreationInformation(); private static void updateFields() { clientContext.Load(list); FieldCollection fields = list.Fields; clientContext.Load(fields); clientContext.Load(list.RootFolder); ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery()); clientContext.Load(listItems); clientContext.ExecuteQuery(); foreach (var listItem in listItems) { Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]); clientContext.Load(listItem.File); clientContext.ExecuteQuery(); Console.WriteLine("listItem File Name: {0}", listItem.File.Name); if (listItem.File.Name.Contains("Sunset")) { ////???????? } listItem.Update(); } clientContext.ExectueQuery(); } 

I know how to get to the field, but I'm not sure how to access the actual value in the field and change it. Does anyone have experience using a client-object model? Thanks for any help that was offered!

+6
source share
4 answers

Updating a field using the client object model is pretty simple:

 ClientContext ctx = new ClientContext("http://yoursite"); List list = ctx.Web.Lists.GetByTitle("ListName"); ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery()); ctx.Load(items); // loading all the fields ctx.ExecuteQuery(); foreach(var item in items) { // important thing is, that here you must have the right type // ie item["Modified"] is DateTime item["fieldName"] = newValue; // do whatever changes you want item.Update(); // important, rembeber changes } ctx.ExecuteQuery(); // important, commit changes to the server 

In DocumentLibrary, this is pretty efficient - you get the same ListItem objects, but you must use the item.File property to access the linked file. Thus, ListItem will contain the values โ€‹โ€‹of the fields, listItem.File will contain a file, say, an image.
And do not forget - to access this file you must Load() it and then ExecuteQuery() .

+14
source

FieldCollection describes the layout of list items . You need to load the actual list items in order to access their values!

Check out this detailed walkthrough on MSDN for more information on SharePoint and the client object model: http://msdn.microsoft.com/en-us/library/ee857094.aspx#SP2010ClientOM_The_Managed_Client_Object_Model

So, in this case, the following sample code on the above MSDN page illustrates this:

 using System; using Microsoft.SharePoint.Client; class Program { static void Main() { ClientContext clientContext = new ClientContext("http://intranet.contoso.com"); List list = clientContext.Web.Lists.GetByTitle("Announcements"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View/>"; ListItemCollection listItems = list.GetItems(camlQuery); clientContext.Load(list);clientContext.Load(listItems); clientContext.ExecuteQuery(); foreach (ListItem listItem in listItems) Console.WriteLine("Id: {0} Title: {1}", listItem.Id, oListItem["Title"]); } } 

In particular, the value of the registered list item is obtained as follows:

 string itemTitle = oListItem["Title"]; 

It uses .NET index syntax.

+1
source

Just remember that each field has an internal name. When you request the index usage index value, you must provide it with the internal name, not the one we see in SharePoint Online.

For example, you add a column called Phone, you should request a value similar to this:

 //query the internal name of the "Phone" field Field field = list.Fields.GetByInternalNameOrTitle("Phone"); context.Load(field); context.ExecuteQuery(); //load items var items = list.GetItems(new CamlQuery()); context.Load(items); context.ExecuteQuery(); foreach (var item in items) { //here we use the internal name Console.WriteLine("\t field, phone:{0}", item[field.InternalName]); } 
+1
source

Hope this helps someone hit the road. Thank you all for your input!

 private static void updateFields() { //Loads the site list clientContext.Load(list); //Creates a ListItemCollection object from list ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery()); //Loads the listItems clientContext.Load(listItems); //Executes the previous queries on the server clientContext.ExecuteQuery(); //For each listItem... foreach (var listItem in listItems) { //Writes out the item ID and Title Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]); //Loads the files from the listItem clientContext.Load(listItem.File); //Executes the previous query clientContext.ExecuteQuery(); //Writes out the listItem File Name Console.WriteLine("listItem File Name: {0}", listItem.File.Name); //Looks for the most recently uploaded file, if found... if (listItem.File.Name.Contains("Sunset")) { //Changes the Title field value listItem["Title"] = title; //Changes the Keywords field value listItem["Keywords"] = keywords1 + keywords2 + keywords3; //Writes out the item ID, Title, and Keywords Console.WriteLine("Id: {0} Title: {1} Keywords: {2}", listItem.Id, listItem["Title"], listItem["Keywords"]); } //Remember changes... listItem.Update(); } //Executes the previous query and ensures changes are committed to the server clientContext.ExecuteQuery(); } 
0
source

Source: https://habr.com/ru/post/894322/


All Articles