简体   繁体   English

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

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

I have a list with 2 text fields, and a choice field. 我有一个包含2个文本字段的列表和一个选择字段。 How do I use the Lists.asmx web service to insert a new item? 如何使用Lists.asmx Web服务插入新项? I can make a web reference to the lists.asmx service, so you can assume that this is known. 我可以对lists.asmx服务进行Web引用,因此您可以假设这是已知的。

I would like a complete example including code and the XML for the CAML query. 我想要一个完整的例子,包括代码和CAML查询的XML。 Ideally the sample would use C#. 理想情况下,样本将使用C#。

Using the Lists web service to insert item into a SharePoint list can indeed be tricky. 使用列表Web服务将项目插入SharePoint列表确实很棘手。 Since this method is of the form: XML in, XML out, it can be hard to get the parameters right. 由于此方法的格式为:XML in,XML out,因此很难获得正确的参数。

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. 字段名称将永远不会与您在SharePoint GUI中看到的字段名称相同。 A good example is the Title field which is used for the first field of the list. 一个很好的例子是Title字段,它用于列表的第一个字段。

Now that you know that, you can create the query to go to SharePoint. 既然您知道这一点,就可以创建查询以转到SharePoint。 An example: 一个例子:

<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. Batch元素是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". 这些应该获得一个唯一的ID(用于向您报告错误)和一个命令,例如可以是“新建”或“更新”。 Inside the Method, you put Field elements that specify the value for each field. 在Method中,您可以放置​​Field元素,为每个字段指定值。 For instance, the Title field gets the value "Abcdef". 例如,Title字段获取值“Abcdef”。 Be careful to use the exact name as it is returned by GetList(). 请小心使用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. 返回值是包含每个更新状态的XML片段。 For instance: 例如:

<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. 此包装类是内部实用程序的一部分,该实用程序用于从项目数据库中检索信息并将其发布到SharePoint。 Since it was developed during company time, I'm not allowed to post it here. 由于它是在公司期间开发的,我不允许在这里发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用.asmx Web服务将项目添加到Sharepoint列表 - How to list Add items to Sharepoint list using .asmx web service 如何通过使用Web服务获取SharePoint列表中新插入的行的ID - How to get the ID of the new inserted row in the SharePoint list by using web service 使用Web服务遍历Sharepoint列表项的属性,得到“对象引用未设置为对象的实例” - Iterating through attributes of Sharepoint list items using web service, getting “Object reference not set to an instance of an object” 如何通过Sharepoint Web服务在自定义列表中添加带有图像的新列表项? - How to add new list item with Image in custom list through sharepoint web service? 如何使用List.asmx本机WebService和客户端对象模型将新项目添加到SharePoint列表? - How to add new items to SharePoint List using List.asmx native WebService and client object model? 如何使用 Sharepoint Web 服务从 .NET 获取文档列表? - How do I use the Sharepoint Web Service to get a list of documents from .NET? 如何将项目列表传递到Web服务 - How to pass list of items to Web Service 如何在使用SharePoint Web服务时仅返回列表的某些列? - How to Return Only Certain Columns of a List When using the SharePoint Web Service? 使用 SharePoint Copy Web 服务时如何设置托管元数据字段? - How do you set a Managed Metadata field when using SharePoint Copy Web Service? 如何使用GraphAPI的c#sdk获取SharePoint列表项? - How do you get SharePoint List Items using the c# sdk for the GraphAPI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM