protected void btnAnoLink_Click(object sender, EventArgs e) { // Get full URL to the document if (documents.Items.Count > 0) { var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var ctx = spContext.CreateUserClientContextForSPHost()) { // Check if this is edit link or not ExternalSharingDocumentOption isEditLink = ShouldLinkBeEdit(); // Get file link URL string fileUrl = ResolveShareUrl(ctx, documents.SelectedValue); // Create anonymous link for document string link = ctx.Web.CreateAnonymousLinkForDocument(fileUrl, isEditLink); // Output the created link lblStatus.Text = string.Format("Created link: {0}", link); } } else { lblStatus.Text = "No document selected to be shared."; } }
/// <summary> /// Share document with complex JSON string value. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="peoplePickerInput">People picker JSON string value containing the target person information</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> public static SharingResult ShareDocumentWithPeoplePickerValue(this Web web, string urlToDocument, string peoplePickerInput, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared for you.", bool useSimplifiedRoles = true) { var groupId = 0; // Set groupId to 0 for external share var propageAcl = false; // Not relevant for external accounts string emailSubject = null; // Not relevant, since we can't change subject var includedAnonymousLinkInEmail = false; // Check if this has any meaning in first place // Set role value accordingly based on requested share option - These are constant in the server side code. var roleValue = ""; switch (shareOption) { case ExternalSharingDocumentOption.Edit: roleValue = "role:1073741827"; break; default: // Use this for other options - Means View permission roleValue = "role:1073741826"; break; } // Share the document, send email and return the result value var result = Web.ShareObject(web.Context, urlToDocument, peoplePickerInput, roleValue, groupId, propageAcl, sendEmail, includedAnonymousLinkInEmail, emailSubject, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return(result); }
/// <summary> /// Creates anonymous link to the given document with automatic expiration time. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <param name="expireTime">Date time for link expiration - will be converted to ISO 8601 format automatically</param> /// <returns>Anonymous URL to the file as string</returns> /// <see cref="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslinkwithexpiration.aspx"/> public static string CreateAnonymousLinkWithExpirationForDocument(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption, DateTime expireTime) { // If null given as expiration, there will not be automatic expiration time string expirationTimeAsString = null; if (expireTime != null) { expirationTimeAsString = expireTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture); } bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } // Get the link ClientResult<string> result = Microsoft.SharePoint.Client.Web.CreateAnonymousLinkWithExpiration( web.Context, urlToDocument, isEditLink, expirationTimeAsString); web.Context.ExecuteQuery(); // Return anonymous link to caller return result.Value; }
/// <summary> /// Abstracted method for sharing documents just with given email address. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="targetEmailToShare">Email address for the person to whom the document will be shared</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <see cref="ShareDocument(Web, string, string, ExternalSharingDocumentOption, bool, string, bool)"/> /// <returns>A SharingResult object</returns> public static SharingResult ShareDocument(this Web web, string urlToDocument, string targetEmailToShare, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared", bool useSimplifiedRoles = true) { return(Task.Run(() => ShareDocumentImplementation(web, urlToDocument, targetEmailToShare, shareOption, sendEmail, emailBody, useSimplifiedRoles)).GetAwaiter().GetResult()); }
protected void btnShareDocument_Click(object sender, EventArgs e) { if (txtTargetEmail.Text.Length == 0) { lblStatus.Text = "Please assign the email address for the person to share the document."; return; } // Get full URL to the document if (documents.Items.Count > 0) { var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var ctx = spContext.CreateUserClientContextForSPHost()) { // Check if this is edit link or not ExternalSharingDocumentOption shareOption = ShouldLinkBeEdit(); // Get file link URL string fileUrl = ResolveShareUrl(ctx, documents.SelectedValue); // Share document for given email address SharingResult result = ctx.Web.ShareDocument(fileUrl, txtTargetEmail.Text, shareOption, true, "Here's your important document"); // Output the created link lblStatus.Text = string.Format("Document sharing status: {0}", result.StatusCode); } } else { lblStatus.Text = "No document selected to be shared."; } }
/// <summary> /// Abstracted methid for sharing documents just with given email address. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="targetEmailToShare">Email address for the person to whom the document will be shared</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <see cref="ShareDocument(Web, string, string, ExternalSharingDocumentOption, bool, string, bool)"/> public static SharingResult ShareDocument(this Web web, string urlToDocument, string targetEmailToShare, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared", bool useSimplifiedRoles = true) { // Resolve people picker value for given email string peoplePickerInput = ResolvePeoplePickerValueForEmail(web, targetEmailToShare); // Share document for user return(ShareDocumentWithPeoplePickerValue(web, urlToDocument, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
/// <summary> /// Creates anonymous link to given document. /// See <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslink.aspx">MSDN</a> /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <returns>Anonymous URL to the file as string</returns> public static string CreateAnonymousLinkForDocument(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption) { bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } ClientResult<string> result = Microsoft.SharePoint.Client.Web.CreateAnonymousLink(web.Context, urlToDocument, isEditLink); web.Context.ExecuteQueryRetry(); // return anonymous link to caller return result.Value; }
private static async Task <SharingResult> ShareDocumentImplementation(Web web, string urlToDocument, string targetEmailToShare, ExternalSharingDocumentOption shareOption, bool sendEmail, string emailBody, bool useSimplifiedRoles) { // Resolve people picker value for given email string peoplePickerInput = await ResolvePeoplePickerValueForEmailImplementation(web, targetEmailToShare); // Share document for user return(await ShareDocumentWithPeoplePickerValueImplementation(web, urlToDocument, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
/// <summary> /// Abstracted method for sharing documents just with given email address. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="targetEmailToShare">Email address for the person to whom the document will be shared</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <see cref="ShareDocumentAsync(Web, string, string, ExternalSharingDocumentOption, bool, string, bool)"/> /// <returns>A SharingResult object</returns> public static async Task <SharingResult> ShareDocumentAsync(this Web web, string urlToDocument, string targetEmailToShare, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared", bool useSimplifiedRoles = true) { await new SynchronizationContextRemover(); return(await ShareDocumentImplementation(web, urlToDocument, targetEmailToShare, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
private static async Task <string> CreateAnonymousLinkWithExpirationForDocumentImplementation(Web web, string urlToDocument, ExternalSharingDocumentOption shareOption, DateTime expireTime) { // If null given as expiration, there will not be automatic expiration time var expirationTimeAsString = expireTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture); bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } // Get the link var result = Web.CreateAnonymousLinkWithExpiration(web.Context, urlToDocument, isEditLink, expirationTimeAsString); await web.Context.ExecuteQueryRetryAsync(); // Return anonymous link to caller return(result.Value); }
/// <summary> /// Creates anonymous link to the given document with automatic expiration time. /// See <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslinkwithexpiration.aspx">MSDN</a> /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <param name="expireTime">Date time for link expiration - will be converted to ISO 8601 format automatically</param> /// <returns>Anonymous URL to the file as string</returns> public static async Task <string> CreateAnonymousLinkWithExpirationForDocumentAsync(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption, DateTime expireTime) { await new SynchronizationContextRemover(); return(await CreateAnonymousLinkWithExpirationForDocumentImplementation(web, urlToDocument, shareOption, expireTime)); }
/// <summary> /// Creates anonymous link to the given document with automatic expiration time. /// See <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslinkwithexpiration.aspx">MSDN</a> /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <param name="expireTime">Date time for link expiration - will be converted to ISO 8601 format automatically</param> /// <returns>Anonymous URL to the file as string</returns> public static string CreateAnonymousLinkWithExpirationForDocument(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption, DateTime expireTime) { return(Task.Run(() => CreateAnonymousLinkWithExpirationForDocumentImplementation(web, urlToDocument, shareOption, expireTime)).GetAwaiter().GetResult()); }
private static async Task <string> CreateAnonymousLinkForDocumentImplementation(Web web, string urlToDocument, ExternalSharingDocumentOption shareOption) { bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } var result = Web.CreateAnonymousLink(web.Context, urlToDocument, isEditLink); await web.Context.ExecuteQueryRetryAsync(); // return anonymous link to caller return(result.Value); }
/// <summary> /// Share document with complex JSON string value. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="peoplePickerInput">People picker JSON string value containing the target person information</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns>A SharingResult object</returns> public static SharingResult ShareDocumentWithPeoplePickerValue(this Web web, string urlToDocument, string peoplePickerInput, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared for you.", bool useSimplifiedRoles = true) { return(Task.Run(() => ShareDocumentWithPeoplePickerValueImplementation(web, urlToDocument, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles)).GetAwaiter().GetResult()); }
/// <summary> /// Creates anonymous link to the given document with automatic expiration time. /// See <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslinkwithexpiration.aspx">MSDN</a> /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <param name="expireTime">Date time for link expiration - will be converted to ISO 8601 format automatically</param> /// <returns>Anonymous URL to the file as string</returns> public static string CreateAnonymousLinkWithExpirationForDocument(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption, DateTime expireTime) { // If null given as expiration, there will not be automatic expiration time string expirationTimeAsString = null; if (expireTime != null) { expirationTimeAsString = expireTime.ToString("s", System.Globalization.CultureInfo.InvariantCulture); } bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } // Get the link ClientResult <string> result = Microsoft.SharePoint.Client.Web.CreateAnonymousLinkWithExpiration( web.Context, urlToDocument, isEditLink, expirationTimeAsString); web.Context.ExecuteQueryRetry(); // Return anonymous link to caller return(result.Value); }
/// <summary> /// Share document with complex JSON string value. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="peoplePickerInput">People picker JSON string value containing the target person information</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> public static SharingResult ShareDocumentWithPeoplePickerValue(this Web web, string urlToDocument, string peoplePickerInput, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared for you.", bool useSimplifiedRoles = true) { int groupId = 0; // Set groupId to 0 for external share bool propageAcl = false; // Not relevant for external accounts string emailSubject = null; // Not relevant, since we can't change subject bool includedAnonymousLinkInEmail = false; // Check if this has any meaning in first place // Set role value accordingly based on requested share option - These are constant in the server side code. string roleValue = ""; switch (shareOption) { case ExternalSharingDocumentOption.Edit: roleValue = "role:1073741827"; break; default: // Use this for other options - Means View permission roleValue = "role:1073741826"; break; } // Share the document, send email and return the result value SharingResult result = Microsoft.SharePoint.Client.Web.ShareObject(web.Context, urlToDocument, peoplePickerInput, roleValue, groupId, propageAcl, sendEmail, includedAnonymousLinkInEmail, emailSubject, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return result; }
/// <summary> /// Abstracted methid for sharing documents just with given email address. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="targetEmailToShare">Email address for the person to whom the document will be shared</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <see cref="ShareDocument(Web, string, string, ExternalSharingDocumentOption, bool, string, bool)"/> public static SharingResult ShareDocument(this Web web, string urlToDocument, string targetEmailToShare, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared", bool useSimplifiedRoles = true) { // Resolve people picker value for given email string peoplePickerInput = ResolvePeoplePickerValueForEmail(web, targetEmailToShare); // Share document for user return ShareDocumentWithPeoplePickerValue(web, urlToDocument, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles); }
/// <summary> /// Share document with complex JSON string value. /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="peoplePickerInput">People picker JSON string value containing the target person information</param> /// <param name="shareOption">View or Edit option</param> /// <param name="sendEmail">Send email or not</param> /// <param name="emailBody">Text attached to the email sent for the person to whom the document is shared</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns>A SharingResult object</returns> public static async Task <SharingResult> ShareDocumentWithPeoplePickerValueAsync(this Web web, string urlToDocument, string peoplePickerInput, ExternalSharingDocumentOption shareOption, bool sendEmail = true, string emailBody = "Document shared for you.", bool useSimplifiedRoles = true) { await new SynchronizationContextRemover(); return(await ShareDocumentWithPeoplePickerValueImplementation(web, urlToDocument, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
/// <summary> /// Creates anonymous link to given document. /// See <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.createanonymouslink.aspx">MSDN</a> /// </summary> /// <param name="web">Web for the context used for people picker search</param> /// <param name="urlToDocument">Full URL to the file which is shared</param> /// <param name="shareOption">Type of the link to be created - View or Edit</param> /// <returns>Anonymous URL to the file as string</returns> public static string CreateAnonymousLinkForDocument(this Web web, string urlToDocument, ExternalSharingDocumentOption shareOption) { bool isEditLink = true; switch (shareOption) { case ExternalSharingDocumentOption.Edit: isEditLink = true; break; case ExternalSharingDocumentOption.View: isEditLink = false; break; default: break; } var result = Web.CreateAnonymousLink(web.Context, urlToDocument, isEditLink); web.Context.ExecuteQueryRetry(); // return anonymous link to caller return(result.Value); }