gpt4 book ai didi

c# - SharePoint 2010 - 客户端对象模型 - 将附件添加到 ListItem

转载 作者:可可西里 更新时间:2023-11-01 07:51:39 24 4
gpt4 key购买 nike

我有一个 SharePoint 列表,我正在使用客户端对象模型向其中添加新的 ListItem。添加 ListItems 不是问题,而且效果很好。

现在我要添加附件。

我按以下方式使用 SaveBinaryDirect:

File.SaveBinaryDirect(clientCtx, url.AbsolutePath + "/Attachments/31/" + fileName, inputStream, true);

只要我尝试添加附件的项目已经具有通过 SharePoint 站点添加的附件且未使用客户端对象模型,它就可以正常工作。

当我尝试向尚无任何附件的项目添加附件时,我收到以下错误(两者都发生但不是针对相同的文件 - 但是这两条消息始终显示):

The remote server returned an error: (409) Conflict
The remote server returned an error: (404) Not Found

I figured that maybe I need to create the attachment folder first for this item. When I try the following code:

clientCtx.Load(ticketList.RootFolder.Folders);
clientCtx.ExecuteQuery();
clientCtx.Load(ticketList.RootFolder.Folders[1]); // 1 -> Attachment folder
clientCtx.Load(ticketList.RootFolder.Folders[1].Folders);
clientCtx.ExecuteQuery();
Folder folder = ticketList.RootFolder.Folders[1].Folders.Add("33");
clientCtx.ExecuteQuery();

我收到一条错误消息:

Cannot create folder "Lists/Ticket System/Attachment/33"

我拥有 SharePoint 站点/列表的完全管理员权限。

知道我可能做错了什么吗?

谢谢,索本

最佳答案

我也为这个问题苦苦挣扎了很长时间,所以我想我应该发布一个完整的代码示例来展示如何成功地创建一个列表项并添加一个附件。

我正在使用客户端对象 API 创建列表项,并使用 SOAP Web 服务添加附件。这是因为,如网络上其他地方所述,客户端对象 API 只能用于将附件添加到项目的上传目录已经存在的项目(例如,如果项目已经有附件)。否则它会因 409 错误或其他原因而失败。不过,SOAP Web 服务可以解决这个问题。

请注意,我必须克服的另一件事是,即使我使用以下 URL 添加了 SOAP 引用:

https://my.sharepoint.installation/personal/test/_vti_bin/lists.asmx

VS 实际添加到 app.config 的 URL 是:

https://my.sharepoint.installation/_vti_bin/lists.asmx

我必须手动将 app.config 更改回正确的 URL,否则我会收到错误消息:

List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user. 0x82000006

代码如下:

    void CreateWithAttachment()
{
const string listName = "MyListName";
// set up our credentials
var credentials = new NetworkCredential("username", "password", "domain");

// create a soap client
var soapClient = new ListsService.Lists();
soapClient.Credentials = credentials;

// create a client context
var clientContext = new Microsoft.SharePoint.Client.ClientContext("https://my.sharepoint.installation/personal/test");
clientContext.Credentials = credentials;

// create a list item
var list = clientContext.Web.Lists.GetByTitle(listName);
var itemCreateInfo = new ListItemCreationInformation();
var newItem = list.AddItem(itemCreateInfo);

// set its properties
newItem["Title"] = "Created from Client API";
newItem["Status"] = "New";
newItem["_Comments"] = "here are some comments!!";

// commit it
newItem.Update();
clientContext.ExecuteQuery();

// load back the created item so its ID field is available for use below
clientContext.Load(newItem);
clientContext.ExecuteQuery();

// use the soap client to add the attachment
const string path = @"c:\temp\test.txt";
soapClient.AddAttachment(listName, newItem["ID"].ToString(), Path.GetFileName(path),
System.IO.File.ReadAllBytes(path));
}

希望这对某人有帮助。

关于c# - SharePoint 2010 - 客户端对象模型 - 将附件添加到 ListItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2969810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com