按名称从共享点获取文件 url [英] Get file url from sharepoint by name

查看:48
本文介绍了按名称从共享点获取文件 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将文档保存在 Sharepoint 文档中.保存文档后,我希望能够获取该文档的网址,以便与用户共享该网址.

I'm able to save document in Sharepoint documents. Once the document is saved I want to be able to get the url of that document so I can share the url with a user.

这是我用来保存文档的代码:

This is the code I'm using to save the document:

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();
}

上传后如何获取该文档的 URL?

How I can get the URL of that document once it's uploaded?

推荐答案

您可以使用以下 3 种方法之一.要共享文档,您可以生成匿名链接、带有到期日期的匿名链接或通过电子邮件共享和发送.

You can use either of the below 3 methods. To share a document, you can generate anonymous link, anon link with expiration date or share and send it via email.

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");

确保外部共享功能已开启.

Ensure that external sharing capability is turned on.

参考 - 在 SPO 中开启外部共享

这篇关于按名称从共享点获取文件 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆