나는 더 이상의 아이디어가 없습니다. 셰어 포인트 목록에 새 요소를 만들고 파일을 첨부 파일로 추가하려고합니다. ListItem
으로 새 요소를 만들 수 있습니다. 그러나 파일은 업로드되지 않습니다.첨부 파일을 Microsoft.SharePoint.Client.ListItem에 첨부 파일로 추가 (SharePoint 2010)
SaveBinaryDirect()
기능과 AttachmentCreationInformation()
클래스로 시도합니다. 내가하려고하면 나는
예외가 throw이 예외를 얻을 : 'System.Net.WebException'을 System.dll을에 (400)
가 나는 또한 SharePoint 서버에 연결하고 Windows 로그 내부 모습 ,하지만 나는 아무것도 발견하지 못했습니다.
내 코드에 ListItem
에 lLibrary.AddItem(itemCreateInfo)
을 새로 추가합니다. 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
에서 찾을 수 있습니다.하지만 이제 Exception 500이 생겼습니다. 그래서 Test SharePoint에 연결하려고했습니다. 메시지가있는 로그 정보를 읽을 수 있습니다. 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()
에 해당 기능을 지정해야하는지 알 수 없습니다.
나의 새로운 코드 :
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;
}
https : //www.codeproject.com/Questions/602369/C-pluscodeplustoplusuploadplusdocumentploploplus – saj