[英]How to Serialize Microsoft.SharePoint.Client.ListItem (Sharepoint-2013)?
[英]C# - Add a File as Attachment to Microsoft.SharePoint.Client.ListItem (SharePoint 2010)
我沒有更多的想法。 我想為 Sharepoint 列表創建一個新元素並將文件添加為附件。 我可以用ListItem
創建一個新元素。 但是文件不會上傳。
我嘗試使用SaveBinaryDirect()
函數和AttachmentCreationInformation()
類。 如果我嘗試我得到這個例外
拋出異常:System.dll 中的“System.Net.WebException”(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 - 客戶端對象模型 - 添加附件到 ListItem 中找到更多信息
但現在我收到了 Exception 500。這就是我嘗試連接到測試 SharePoint 的原因。 在那里我可以使用 Message The list does not exist. The selected page contains a list that does not exist. The list could have been deleted by another user.
讀取日志信息The list does not exist. The selected page contains a list that does not exist. The list could have been deleted by another user.
The list does not exist. The selected page contains a list that does not exist. The list could have been deleted by another user.
我不知道我必須為找到列表的函數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
,端點地址顯式引用了站點。
“配置”文件的一部分:
<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>
在標簽“端點”是屬性“地址”。 這包含網絡參考的地址。 但正確的地址必須是address="https://TestSharePoint.com/TestSite/TestUnderSite/_vti_bin/lists.asmx"
。
請嘗試以下代碼。 修改了文件夾 url 的形成方式。 還修改了一些與附件上傳相關的代碼。
在sUrl
,請傳遞您網站集的完整網址。
var list = web.Lists.GetByTitle(sLibrary);
clientContext.Load(list);
clientContext.ExecuteQuery();
ListItemCreationInformation listItemCreationInformation = null;
if (!string.IsNullOrEmpty(sFolderName))
{
listItemCreationInformation = new ListItemCreationInformation();
listItemCreationInformation.FolderUrl = string.Format("{0}Lists/{1}/{2}", sURL, sLibrary, sFolderName);
}
var listItem = list.AddItem(listItemCreationInformation);
newItem["Title"] = sTitel;
newItem["Description"] = sDescription;
listItem.Update();
clientContext.ExecuteQuery();
using (FileStream fs = new FileStream(sFilePath, FileMode.Open))
{
var attInfo = new AttachmentCreationInformation();
attInfo.FileName = fs.Name;
attInfo.ContentStream = fs;
var att = listItem.AttachmentFiles.Add(attInfo);
clientContext.Load(att);
clientContext.ExecuteQuery();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.