使用客户端对象模型更新 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();
}
我知道如何访问该字段,但我不确定如何访问该字段中的实际值并修改它。有人有使用客户端对象模型的经验吗?感谢您提供的任何帮助!
So I am trying to create a method that is essentially used to change the value of a field in SharePoint.
This is what I have so far...
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 am not sure how to access the actual value within the field and modify it. Does anyone have any experience with this using the client-object model? Thank you for any help that's offered!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用客户端对象模型更新字段非常简单:
使用 DocumentLibrary 则完全不同 - 您可以获得相同的 ListItem 对象,但要访问关联的文件,您必须使用
item.File
属性。因此 ListItem 本身将包含字段值,listItem.File
将包含文件,例如图像。并且不要忘记 - 要访问该文件,您必须
Load()
它,然后ExecuteQuery()
。Updating of a field with Client Object Model is pretty straight forward:
With DocumentLibrary it is quite defferent - you get those same ListItem objects, but to access the associated file you must use
item.File
property. So ListItem itself will contain field values,listItem.File
will contain file, say image.And don't forget - to access that file you must
Load()
it and thenExecuteQuery()
.请记住,每个字段都有一个内部名称。当您使用索引器查询字段值时,您应该为其指定内部名称,而不是我们在 SharePoint Online 中看到的名称。
例如,您添加一个名为“电话”的列,您应该像这样查询该值:
Just remember that every field has an internal name. When you query the field value use indexer, you should give it the internal name, not the one we see in the SharePoint Online.
For example, you add a column named Phone, You should query the value like this:
FieldCollection 描述列表项的架构。您需要加载实际列表项才能访问它们的值!
查看 MSDN 上的详细演练,了解有关 SharePoint 和客户端对象模型的更多背景信息:http://msdn.microsoft.com/en-us/library/ee857094.aspx#SP2010ClientOM_The_Managed_Client_Object_Model
因此,在这种情况下,上述 MSDN 页面中的以下示例代码说明了这一点
:列表项字段值的检索方式如下:
它使用 .NET 索引器语法。
The FieldCollection describes the schema of the list items. You need to load actual list items to access their values!
Check out this detailed walkthrough on MSDN for more background 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 from the above MSDN page illustrates it:
In particular the value of a list item filed is retrieved as follows:
It uses .NET indexer syntax.
希望这可以帮助某人走出困境。感谢大家的意见!
Hope this helps someone out down the road. Thanks everyone for your input!