使用客户-对象模型更新SharePoint中的字段值



所以我试图创建一个方法,基本上是用来改变SharePoint字段的值。

这是我到目前为止所做的…

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();
}

我知道如何到达字段,但我不确定如何访问字段内的实际值并修改它。有人有使用客户-对象模型的经验吗?谢谢你提供的任何帮助!

使用客户端对象模型更新字段非常简单:

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
    // i.e. 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

与DocumentLibrary是完全不同的-你得到那些相同的ListItem对象,但要访问相关的文件,你必须使用item.File属性。因此,listtitem本身将包含字段值,listItem.File将包含文件,例如image。
不要忘记-要访问该文件,您必须Load()它,然后ExecuteQuery()

请记住,每个字段都有一个内部名称。当你使用indexer查询字段值时,你应该给它一个内部名称,而不是我们在SharePoint Online中看到的那个。

例如,您添加一个名为Phone的列,您应该像这样查询值:

//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]);
}

FieldCollection描述了列表项的模式。您需要加载实际的列表项来访问它们的值!

查看MSDN上的详细操作,了解SharePoint和客户端对象模型的更多背景知识:http://msdn.microsoft.com/en-us/library/ee857094.aspx#SP2010ClientOM_The_Managed_Client_Object_Model

因此,在这种情况下,以下示例代码来自上述MSDN页面说明:

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"]);
    }
}

具体来说,列表项字段的值按如下方式检索:

string itemTitle = oListItem["Title"];

使用。net索引器语法

希望这对将来的人有所帮助。谢谢大家的意见!

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();
        }

相关内容

  • 没有找到相关文章

最新更新