简体   繁体   English

通过名称从共享点获取文件URL

[英]Get file url from sharepoint by name

I'm able to save document in Sharepoint documents. 我可以将文档保存在Sharepoint文档中。 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();
}

How I can get the URL of that document once it's uploaded? 文件上载后,如何获取该URL?

You can use either of the below 3 methods. 您可以使用以下3种方法之一。 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. 确保外部共享功能已打开。

Reference - Turn on external sharing in SPO 参考- 在SPO中打开外部共享

To get url of uploaded file you could utilize the following properties: 要获取上传文件的网址,您可以利用以下属性:

  • EncodedAbsUrl - gets the absolute url of file EncodedAbsUrl获取文件的绝对URL
  • EncodedAbsUrl - gets the server relative url of file EncodedAbsUrl获取文件的服务器相对URL

They need to requested explicitly once the file is getting uploaded like this: 文件上传后,他们需要明确请求:

var uploadFile = list.RootFolder.Files.Add(fileInfo);
ctx.Load(uploadFile.ListItemAllFields,item => item["EncodedAbsUrl"], item => item["FileRef"]);
ctx.ExecuteQuery();

Example

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

Update 更新

To get the url of display form of document you could 要获取文档显示形式的网址,您可以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM