7

そのため、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();
}

フィールドにアクセスする方法は知っていますが、フィールド内の実際の値にアクセスして変更する方法がわかりません。クライアントオブジェクトモデルを使用した経験はありますか?提供された助けをありがとう!

4

4 に答える 4

15

クライアント オブジェクト モデルを使用したフィールドの更新は非常に簡単です。

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プロパティを使用する必要があります。したがって、ListItem 自体にはフィールド値listItem.Fileが含まれ、ファイル、たとえば画像が含まれます。
そして、忘れないでください - そのファイルにアクセスするにはLoad()、それからExecuteQuery().

于 2011-08-05T09:20:07.107 に答える
3

すべてのフィールドには内部名があることに注意してください。インデクサーを使用してフィールド値を照会する場合は、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]);
}
于 2014-08-06T08:36:00.870 に答える
1

FieldCollectionは、リストアイテムのスキーマを記述します。それらの値にアクセスするには、実際のリストアイテムをロードする必要があります。

SharePointとクライアントオブジェクトモデルの背景については、MSDNのこの詳細なウォークスルーを確認してください: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インデクサー構文を使用します。

于 2011-08-04T19:49:44.777 に答える
0

これが誰かの助けになることを願っています。ご意見をお寄せいただきありがとうございます。

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();
        }
于 2011-08-05T16:05:52.497 に答える