C# SharePoint - 如何使用列表 Web 服务插入新项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/85392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:25:12  来源:igfitidea点击:

SharePoint - How do insert new items using the list web service?

提问by Brian Lyttle

I have a list with 2 text fields, and a choice field. How do I use the Lists.asmx web service to insert a new item? I can make a web reference to the lists.asmx service, so you can assume that this is known.

我有一个包含 2 个文本字段和一个选择字段的列表。如何使用 Lists.asmx Web 服务插入新项目?我可以对lists.asmx 服务进行网络引用,因此您可以假设这是已知的。

I would like a complete example including code and the XML for the CAML query. Ideally the sample would use C#.

我想要一个完整的示例,包括 CAML 查询的代码和 XML。理想情况下,示例将使用 C#。

采纳答案by jan.vdbergh

Using the Lists web service to insert item into a SharePoint list can indeed be tricky. Since this method is of the form: XML in, XML out, it can be hard to get the parameters right.

使用列表 Web 服务将项目插入 SharePoint 列表确实很棘手。由于此方法的形式为:XML 输入,XML 输出,因此很难正确获取参数。

First you should take a look at the list definition. It can be retrieved with the method GetList(), as shown below:

首先,您应该查看列表定义。可以使用 GetList() 方法检索它,如下所示:

XmlNode listXml = sharePointLists.GetList(listName);
File.WriteAllText("listdefinition.xml", listXml.OuterXml);

Important here are the names of the fields and their data types. Field names will never be the same as the ones you see in the SharePoint GUI. A good example is the Title field which is used for the first field of the list.

这里重要的是字段的名称及其数据类型。字段名称永远不会与您在 SharePoint GUI 中看到的名称相同。一个很好的例子是标题字段,它用于列表的第一个字段。

Now that you know that, you can create the query to go to SharePoint. An example:

现在您知道了,您可以创建查询以转到 SharePoint。一个例子:

<Batch OnError="Continue">
    <Method ID="1" Cmd="New">
        <Field Name="Title">Abcdef</Field>
        <Field Name="Project_x0020_code">999050</Field>
        <Field Name="Status">Open</Field>    
    </Method>
</Batch>

The Batch element is the root element of the XML. Inside you can put different Methods. These should get a unique ID (which is used to report errors back to you) and a command, which can for instance be "New" or "Update". Inside the Method, you put Field elements that specify the value for each field. For instance, the Title field gets the value "Abcdef". Be careful to use the exact name as it is returned by GetList().

Batch 元素是 XML 的根元素。在里面你可以放不同的方法。这些应该获得一个唯一的 ID(用于向您报告错误)和一个命令,例如可以是“新建”或“更新”。在 Method 内部,您放置了 Field 元素,用于指定每个字段的值。例如,标题字段获取值“Abcdef”。小心使用确切的名称,因为它是由 GetList() 返回的。

To execute the query on SharePoint, use the UpdateListItems() method:

要在 SharePoint 上执行查询,请使用 UpdateListItems() 方法:

XmlNode result = sharePointLists.UpdateListItems(listDefinition.Name, updates);

The return value is an XML fragment containing the status of each update. For instance:

返回值是一个包含每次更新状态的 XML 片段。例如:

<Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <Result ID="1,New">
    <ErrorCode>0x00000000</ErrorCode>
    <z:row ows_ContentTypeId="0x010036F3F587127F1A44B8BA3FEFED4733C6" 
         ows_Title="Abcdef" 
         ows_Project_x0020_code="999050" 
         ows_Status="Open" 
         ows_LinkTitleNoMenu="Abcdef" 
         ows_LinkTitle="Abcdef" 
         ows_ID="1005"            
         ... 
         xmlns:z="#RowsetSchema" />
    </Result>
</Results>

You can parse this and look at the ErrorCode to see which methods failed.

您可以解析它并查看 ErrorCode 以查看哪些方法失败。

In practice I have created a wrapper class that takes care of all the dirty details for me. Unfortunately this is owned by my employer so I cannot share it with you.

在实践中,我创建了一个包装类来为我处理所有脏细节。不幸的是,这是我的雇主所有,所以我不能与你分享。

This wrapper class is part of an internal utility that is used to retrieve information from our project database and post it to SharePoint. Since it was developed during company time, I'm not allowed to post it here.

此包装器类是用于从我们的项目数据库中检索信息并将其发布到 SharePoint 的内部实用程序的一部分。由于是在公司时间开发的,所以不让发在这里。