如何自动获取文件夹中所有文件的共享点链接(Link for 'People with existing access')?
How to automatically get sharepoint links (Link for 'People with existing access') to all files in a folder?
我的 Sharepoint 文件夹中有大量文件,我需要与组织外部有权访问该文件夹的人员共享 link 一些单独的文件。我可以通过手动选择每个文件来获取 link,将 link 选项更改为 'People with existing access' 并单击应用。这会给我一个看起来像这样的 link:
<https://.sharepoint.com/:p:/r/sites/Marketing/Shared%20Documents/Branding%20Elements.pptx?d=w1bae13735fdb4b1d9dd0ae217bc2d911&csf=1&e=aojURR>
但是,对数百个文件执行此操作是不切实际的。有人可以帮忙 easier/quicker solution/automation 吗?
非常感谢!
您可以使用 SharePoint 客户端对象模型 (CSOM) 使用您的用户凭据获取共享 links(如果网站启用了 MFA,您将需要使用应用程序凭据,如所述here):
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
SecureString password = new SecureString();
foreach (char c in "password".ToCharArray()) password.AppendChar(c);
using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
{
string folderUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder";
Uri folderUri = new Uri(folderUrl);
Folder folder = cc.Web.GetFolderByServerRelativeUrl(folderUri.AbsolutePath);
cc.ExecuteQuery();
cc.Load(folder.Files);
cc.ExecuteQuery();
FileCollection fileCol = folder.Files;
foreach (File file in fileCol)
{
Console.WriteLine(file.LinkingUrl);
}
}
}
上面的代码应该给你“具有现有访问权限的人”link。下面的代码显示了如何创建其他类型的 links:
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
SecureString password = new SecureString();
foreach (char c in "password".ToCharArray()) password.AppendChar(c);
//Use the commented out line for App Only Authentication
//using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "ClientID", "ClientSecret"))
using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
{
string fileUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder/File.xlsx";
Uri fileUri = new Uri(fileUrl);
Microsoft.SharePoint.Client.File file = cc.Web.GetFileByServerRelativeUrl(fileUri.AbsolutePath);
cc.Load(file);
//Existing Access Link (i.e. "People with existing access")
//file.LinkingUrl
//Organization Link (i.e. "People in org with link")
bool isEdit = true;
var orgEditLink = Microsoft.SharePoint.Client.Web.CreateOrganizationSharingLink(cc, fileUrl, isEdit);
//Anonymous Link (i.e. "Anyone with the link")
ClientResult<String> anonEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLink(cc, fileUrl, isEdit);
string anonEditLink2 = cc.Web.CreateAnonymousLinkForDocument(fileUrl, ExternalSharingDocumentOption.Edit);
//Anonymous Link with Expiration (not available in GUI)
DateTime five_days = DateTime.Now.AddDays(5);
ClientResult<String> anonExpEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLinkWithExpiration(cc, fileUrl, isEdit, five_days.ToString("yyyy-MM-ddThh:mm:ssZ"));
string anonExpEditLink2 = cc.Web.CreateAnonymousLinkWithExpirationForDocument(fileUrl, ExternalSharingDocumentOption.Edit, five_days);
//User Share Link (i.e. "People you specify can edit")
String target_email = "email@example.com";
bool send_email = false;
SharingResult userEditLink = cc.Web.ShareDocument(fileUrl, target_email, ExternalSharingDocumentOption.Edit, send_email);
cc.ExecuteQuery();
Console.WriteLine(file.LinkingUrl);
Console.WriteLine(orgEditLink.Value);
Console.WriteLine(anonEditLink.Value);
Console.WriteLine(anonEditLink2);
Console.WriteLine(anonExpEditLink.Value);
Console.WriteLine(anonExpEditLink2);
foreach (SPInvitationCreationResult invuser in userEditLink.InvitedUsers)
{
Console.WriteLine(invuser.InvitationLink);
}
}
}
我的 Sharepoint 文件夹中有大量文件,我需要与组织外部有权访问该文件夹的人员共享 link 一些单独的文件。我可以通过手动选择每个文件来获取 link,将 link 选项更改为 'People with existing access' 并单击应用。这会给我一个看起来像这样的 link: <https://.sharepoint.com/:p:/r/sites/Marketing/Shared%20Documents/Branding%20Elements.pptx?d=w1bae13735fdb4b1d9dd0ae217bc2d911&csf=1&e=aojURR>
但是,对数百个文件执行此操作是不切实际的。有人可以帮忙 easier/quicker solution/automation 吗?
非常感谢!
您可以使用 SharePoint 客户端对象模型 (CSOM) 使用您的用户凭据获取共享 links(如果网站启用了 MFA,您将需要使用应用程序凭据,如所述here):
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
SecureString password = new SecureString();
foreach (char c in "password".ToCharArray()) password.AppendChar(c);
using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
{
string folderUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder";
Uri folderUri = new Uri(folderUrl);
Folder folder = cc.Web.GetFolderByServerRelativeUrl(folderUri.AbsolutePath);
cc.ExecuteQuery();
cc.Load(folder.Files);
cc.ExecuteQuery();
FileCollection fileCol = folder.Files;
foreach (File file in fileCol)
{
Console.WriteLine(file.LinkingUrl);
}
}
}
上面的代码应该给你“具有现有访问权限的人”link。下面的代码显示了如何创建其他类型的 links:
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
SecureString password = new SecureString();
foreach (char c in "password".ToCharArray()) password.AppendChar(c);
//Use the commented out line for App Only Authentication
//using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "ClientID", "ClientSecret"))
using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
{
string fileUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder/File.xlsx";
Uri fileUri = new Uri(fileUrl);
Microsoft.SharePoint.Client.File file = cc.Web.GetFileByServerRelativeUrl(fileUri.AbsolutePath);
cc.Load(file);
//Existing Access Link (i.e. "People with existing access")
//file.LinkingUrl
//Organization Link (i.e. "People in org with link")
bool isEdit = true;
var orgEditLink = Microsoft.SharePoint.Client.Web.CreateOrganizationSharingLink(cc, fileUrl, isEdit);
//Anonymous Link (i.e. "Anyone with the link")
ClientResult<String> anonEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLink(cc, fileUrl, isEdit);
string anonEditLink2 = cc.Web.CreateAnonymousLinkForDocument(fileUrl, ExternalSharingDocumentOption.Edit);
//Anonymous Link with Expiration (not available in GUI)
DateTime five_days = DateTime.Now.AddDays(5);
ClientResult<String> anonExpEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLinkWithExpiration(cc, fileUrl, isEdit, five_days.ToString("yyyy-MM-ddThh:mm:ssZ"));
string anonExpEditLink2 = cc.Web.CreateAnonymousLinkWithExpirationForDocument(fileUrl, ExternalSharingDocumentOption.Edit, five_days);
//User Share Link (i.e. "People you specify can edit")
String target_email = "email@example.com";
bool send_email = false;
SharingResult userEditLink = cc.Web.ShareDocument(fileUrl, target_email, ExternalSharingDocumentOption.Edit, send_email);
cc.ExecuteQuery();
Console.WriteLine(file.LinkingUrl);
Console.WriteLine(orgEditLink.Value);
Console.WriteLine(anonEditLink.Value);
Console.WriteLine(anonEditLink2);
Console.WriteLine(anonExpEditLink.Value);
Console.WriteLine(anonExpEditLink2);
foreach (SPInvitationCreationResult invuser in userEditLink.InvitedUsers)
{
Console.WriteLine(invuser.InvitationLink);
}
}
}