私にはもうアイデアはありません。私はSharepoint Listに新しい要素を作成し、ファイルを添付ファイルとして追加したいと考えています。 ListItem
で新しい要素を作成できます。しかし、ファイルはアップロードされません。C# - Microsoft.SharePoint.Client.ListItemに添付ファイルとしてファイルを追加する(SharePoint 2010)
SaveBinaryDirect()
機能とAttachmentCreationInformation()
クラスを試してみます。私がしようとした場合 は私が
例外がスローされた、この例外が出ます:「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;
}
EDIT!:
私は大きなミスを犯してきました。 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.codeprojectcom/Questions/602369/C-pluscodeplustoplusuploadplusdocumentploploplus – saj