我可以在Sharepoint文档中保存文档。保存文档后,我希望能够获取该文档的URL,以便我可以与用户共享该URL。
这是我用来保存文档的代码:
using (ClientContext clientContext = new ClientContext("https://mydomain.sharepoint.com"))
{
SecureString passWord = new SecureString();
foreach (char c in "mypassword".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("myaccount@mydomain.com", passWord);
Web web = clientContext.Web;
FileCreationInformation newFile = new FileCreationInformation();
//newFile.Content = System.IO.File.ReadAllBytes(filePath);
byte[] docData = null;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
docData = ms.ToArray();
}
newFile.Content = docData;
newFile.Url = originalFileName;
List docs = web.Lists.GetByTitle("DOCUMENTS");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.ExecuteQuery();
}
上传后如何获取该文档的网址?
答案 0 :(得分:2)
您可以使用以下3种方法之一。要共享文档,您可以生成匿名链接,带有过期日期或共享的匿名链接,并通过电子邮件发送。
List docs = web.Lists.GetByTitle("DOCUMENTS");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.ExecuteQuery();
clientContext.Load(uploadFile.ListItemAllFields, item => item["EncodedAbsUrl"]);
clientContext.ExecuteQuery();
var fileUrl = uploadFile.ListItemAllFields["EncodedAbsUrl"].ToString();
string link = clientContext.Web.CreateAnonymousLinkForDocument(fileUrl, ExternalSharingDocumentOption.View);
string linkwithExpiration = clientContext.Web.CreateAnonymousLinkWithExpirationForDocument(fileUrl, ExternalSharingDocumentOption.Edit, DateTime.Now.AddMonths(1));
SharingResult result = clientContext.Web.ShareDocument(fileUrl, "someone@example.com", ExternalSharingDocumentOption.View, true, "Doc shared programmatically");
确保已打开外部共享功能。
答案 1 :(得分:0)
要获取上传文件的网址,您可以使用以下属性:
EncodedAbsUrl
- 获取文件的绝对网址EncodedAbsUrl
- 获取文件的服务器相对网址他们需要在文件上传后明确请求:
var uploadFile = list.RootFolder.Files.Add(fileInfo);
ctx.Load(uploadFile.ListItemAllFields,item => item["EncodedAbsUrl"], item => item["FileRef"]);
ctx.ExecuteQuery();
实施例
var fileInfo = new FileCreationInformation();
fileInfo.Content = System.IO.File.ReadAllBytes(filePath);
fileInfo.Url = Path.GetFileName(filePath);
var list = ctx.Web.Lists.GetByTitle(listTitle);
var uploadFile = list.RootFolder.Files.Add(fileInfo);
ctx.Load(uploadFile.ListItemAllFields,item => item["EncodedAbsUrl"], item => item["FileRef"]);
ctx.ExecuteQuery();
Console.WriteLine(uploadFile.ListItemAllFields["EncodedAbsUrl"]);
Console.WriteLine(uploadFile.ListItemAllFields["FileRef"]);
<强>更新强>
要获取文件显示形式的网址
ctx.Load(list, l => l.DefaultDisplayFormUrl);
ctx.Load(uploadFile.ListItemAllFields,item => item.Id);
ctx.Load(ctx.Site, s => s.Url);
ctx.ExecuteQuery();
var itemUrl = String.Format("{0}{1}?ID={2}",ctx.Site.Url,list.DefaultDisplayFormUrl, uploadFile.ListItemAllFields.Id);