gpt4 book ai didi

C# - 将文件作为附件添加到 Microsoft.SharePoint.Client.ListItem (SharePoint 2010)

转载 作者:行者123 更新时间:2023-11-30 17:31:46 25 4
gpt4 key购买 nike

我没有更多的想法。我想为 Sharepoint 列表创建一个新元素并添加一个文件作为附件。我可以使用 ListItem 创建一个新元素。但是文件不会上传。

我尝试使用 SaveBinaryDirect() 函数和 AttachmentCreationInformation() 类。如果我尝试我得到这个异常

Exception thrown: 'System.Net.WebException' in System.dll (400)

我还连接到 SharePoint Server 并查看了 Windows 日志,但我一无所获。

在我的代码中,我使用 lLibrary.AddItem(itemCreateInfo) 添加了一个新的 ListItem。使用 ListItem 我在 SharePoint 中创建了一个新元素。此元素位于 SharePoint 文件夹中。这一切都很好。

我尝试了很多,但没有任何效果。我需要帮助,拜托!

这里是我的完整代码:

public bool UploadToSharepoint(string sURL, string sLibrary, string sFolderName, string sTitel, string sDescription, string sFilePath)
{
using (ClientContext clientContext = new ClientContext(sURL))
{
if (!SetCredentialsInClientContext(clientContext))
{
return false;
}

List lLibrary = clientContext.Web.Lists.GetByTitle(sLibrary);

clientContext.Load(clientContext.Web.Lists);
clientContext.Load(lLibrary, l => l.RootFolder.ServerRelativeUrl);

clientContext.ExecuteQuery();

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
if (!string.IsNullOrEmpty(sFolderName))
{
itemCreateInfo.FolderUrl = lLibrary.RootFolder.ServerRelativeUrl + "/" + sFolderName;
}

ListItem newItem = lLibrary.AddItem(itemCreateInfo);

#region Work only with Document list in SharePoint
//using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
//{
// clientContext.Load(lLibrary.RootFolder);
// clientContext.ExecuteQuery();
// string fileUrl = string.Format("{0}/{1}", lLibrary.RootFolder.ServerRelativeUrl, fi.Name);
// Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
//} // Ende using 'FileStream'
#endregion

using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
{
#region WORK!
newItem["Title"] = sTitel;
newItem["Description"] = sDescription;
newItem.Update();
clientContext.ExecuteQuery();
#endregion

#region SaveBinaryDirect Example NOT WORK
//using (FileStream strm = new FileInfo(sFilePath).Open(FileMode.Open))
//{
// Uri url = new Uri(sURL);
// //Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, url.AbsolutePath + "/Attachments/" + newItem.Id + "/" + fi.Name, strm, true);
// Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, lLibrary.RootFolder.ServerRelativeUrl + "/Attachments/" + newItem.Id + "/" + fi.Name, strm, true);
//}

////Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "", fs, true);
//string serverRelativeUrl = lLibrary.RootFolder.ServerRelativeUrl;
//Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeUrl, fs, true);
#endregion

#region AttachmentCreationInformation Example NOT WORK
AttachmentCreationInformation attInfo = new AttachmentCreationInformation();
attInfo.FileName = fs.Name;
attInfo.ContentStream = fs;
newItem.AttachmentFiles.Add(attInfo);
newItem.Update();
clientContext.ExecuteQuery();
#endregion
}
}
return true;
}

编辑!

我犯了一个大错。 Sharepoint 是 2010 年的。所以 AttachmentFiles.Add() 函数不起作用。

我发现我必须添加服务引用并更改我的代码。如需更多信息,请访问 SharePoint 2010 - Client Object Model - Add attachment to ListItem

但现在我遇到异常 500。这就是我尝试连接到测试 SharePoint 的原因。在那里我可以读取消息 The list does not exist 的日志信息。
所选页面包含不存在的列表。该列表可能已被其他用户删除。

我不知道我必须为找到列表的函数 AddAttachment() 指定什么 listName 属性。

我的新代码:

public bool UploadToSharepoint(string sURL, string sLibrary, string sFolderName, string sTitel, string sDescription, string sFilePath)
{
using (ClientContext clientContext = new ClientContext(sURL))
{
if (!SetzeCredentialsInClientContext(clientContext))
{
return false;
}

List lLibrary = clientContext.Web.Lists.GetByTitle(sLibrary);

clientContext.Load(lLibrary);
clientContext.Load(lLibrary.RootFolder);
clientContext.ExecuteQuery();

ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
if (!string.IsNullOrEmpty(sFolderName))
{
listItemCreationInformation.FolderUrl = lLibrary.RootFolder.ServerRelativeUrl + "/" + sFolderName;
}

var newItem = lLibrary.AddItem(listItemCreationInformation);
newItem["Title"] = sTitel;
newItem.Update();
clientContext.ExecuteQuery();

clientContext.Load(newItem);
clientContext.ExecuteQuery();

TestSP.ListsSoapClient lsc = new TestSP.ListsSoapClient();

if (_cbAutorisierung.Checked)
{
lsc.ClientCredentials.Windows.ClientCredential = new NetworkCredential(tbName.Text, tbPasswort.Text, tbDomain.Text);
}
else
{
lsc.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
}
lsc.AddAttachment(sLibrary, newItem["ID"].ToString(), Path.GetFileName(sFilePath), System.IO.File.ReadAllBytes(sFilePath));

}
return true;
}

最佳答案

我终于开始工作了!

问题在于,在配置文件 App.Config 中,端点地址显式引用了站点。

“config”文件部分:

<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="ListsSoap">
<security>
<transport clientCredentialType="Ntlm" proxyCredentialType="Ntlm" />
</security>
</binding>
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://TestSharePoint.com/_vti_bin/lists.asmx"
binding="basicHttpsBinding" bindingConfiguration="ListsSoap"
contract="SPRivarkom.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>

在标签“端点”处是属性“地址”。这包含 Web 引用的地址。但正确的地址必须是 address="https://TestSharePoint.com/TestSite/TestUnderSite/_vti_bin/lists.asmx"

关于C# - 将文件作为附件添加到 Microsoft.SharePoint.Client.ListItem (SharePoint 2010),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47419671/

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