[英]How to delete list item using SharePoint Online 2013 Rest API in C#
[英]How to attach a attachment file to a list item using REST API in SharePoint 2013
我想問專家。
任何人都知道如何使用SharePoint 2013中的REST API將附件文件附加到列表項? 我搜索了波紋管文件。 但是沒有關於將文件上傳為列表項附件的信息。
http://msdn.microsoft.com/en-us/library/fp142386.aspx
附加信息:
我找到了吼叫文章。
http://chuvash.eu/2013/02/20/rest-api-add-a-plain-text-file-as-an-attachment-to-a-list-item/
根據該文章,它可以使用下面的Javascript代碼將附件文件上傳到列表項。 我想用C#。 我現在正在嘗試,但我仍然沒有成功。
var content = "Hello, this text is inside the file created with REST API";
var digest = $("#__REQUESTDIGEST").val();
var composedUrl = "/_api/web/lists/GetByTitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt')";
$.ajax({
url: composedUrl,
type: "POST",
data: content,
headers: {
"X-RequestDigest": digest
}
})
如何使用.NET來使用SharePoint REST API有幾種方法,其中一些列在下面:
.NET Framework 4.5
) .NET Framework 1.1
) 所有這些都允許使用REST接口在SharePoint Online / SharePoint 2013中執行CRUD操作。
SPWebClient類演示了如何使用WebClient執行CRUD操作。
以下示例演示如何將附件文件添加到SharePoint Online中的List:
var credentials = new SharePointOnlineCredentials(userName, securePassword);
AddAttachmentFile(webUrl, credentials, "Nokia Offices", 1, @"c:\upload\Nokia Head Office in Espoo.jpg");
public static void AddAttachmentFile(string webUrl,ICredentials credentials,string listTitle,int itemId,string filePath)
{
using (var client = new SPWebClient(new Uri(webUrl),credentials))
{
var fileContent = System.IO.File.ReadAllBytes(filePath);
var fileName = System.IO.Path.GetFileName(filePath);
var endpointUrl = string.Format("{0}/_api/web/lists/GetByTitle('{1}')/items({2})/AttachmentFiles/add(FileName='{3}')", webUrl,listTitle,itemId,fileName);
client.UploadFile(new Uri(endpointUrl), fileContent);
}
}
依賴關系:
試着用這個:
var executor = new SP.RequestExecutor(appweburl);
var digest = $("#__REQUESTDIGEST").val();
var content = "Hello, this text is inside the file created with REST API";
executor.executeAsync(
{
url: appweburl + "/_api/web/lists/getbytitle('List1')/items(1)/AttachmentFiles/add(FileName='readme.txt)",
method: "POST",
body: content,
headers: {
"X-RequestDigest": digest
},
success: function(data) {
toastr.success('Document attached successfully.');
},
error: function(err) {
toastr.error('Oops! Document attached created fail.');
}
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.