/// <summary> /// Used to solve right group ID to assign user into - used for the site level sharing. /// </summary> /// <param name="web">Web to be shared externally</param> /// <param name="shareOption">Permissions to be given for the external user</param> /// <returns>group ID</returns> private static async Task <int> SolveGroupIdToShareAsync(Web web, ExternalSharingSiteOption shareOption) { Group group = null; switch (shareOption) { case ExternalSharingSiteOption.Owner: group = web.AssociatedOwnerGroup; break; case ExternalSharingSiteOption.Edit: group = web.AssociatedMemberGroup; break; case ExternalSharingSiteOption.View: group = web.AssociatedVisitorGroup; break; default: group = web.AssociatedVisitorGroup; break; } // Load right group web.Context.Load(group); await web.Context.ExecuteQueryRetryAsync(); // Return group ID return(group.Id); }
/// <summary> /// Share site for a person using complex JSON object for people picker value. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="peoplePickerInput">JSON object with the people picker value</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSiteWithPeoplePickerValue(this Web web, string peoplePickerInput, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve the group id for the shared option based on default groups int groupId = SolveGroupIdToShare(web, shareOption); string roleValue = string.Format("group:{0}", groupId); // Right permission setup // Ensure that web URL has been loaded if (!web.IsObjectPropertyInstantiated("Url")) { web.Context.Load(web, w => w.Url); web.Context.ExecuteQueryRetry(); } // Set default settings for site sharing bool propageAcl = false; // Not relevant for external accounts bool includedAnonymousLinkInEmail = false; // Not when site is shared SharingResult result = Microsoft.SharePoint.Client.Web.ShareObject(web.Context, web.Url, peoplePickerInput, roleValue, 0, propageAcl, sendEmail, includedAnonymousLinkInEmail, null, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return(result); }
/// <summary> /// Share site for a person using complex JSON object for people picker value. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="peoplePickerInput">JSON object with the people picker value</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</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 ShareSiteWithPeoplePickerValue(this Web web, string peoplePickerInput, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { return(Task.Run(() => ShareSiteWithPeoplePickerValueImplementation(web, peoplePickerInput, shareOption, sendEmail, emailBody, useSimplifiedRoles)).GetAwaiter().GetResult()); }
/// <summary> /// Share site for a person using just email. Will resolve needed people picker JSON value automatically. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="email">Email of the person to whom site should be shared.</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</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> ShareSiteAsync(this Web web, string email, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { await new SynchronizationContextRemover(); return(await ShareSiteImplementation(web, email, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
/// <summary> /// Share site for a person using just email. Will resolve needed people picker JSON value automatically. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="email">Email of the person to whom site should be shared.</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSite(this Web web, string email, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve people picker value for email address string peoplePickerValue = ResolvePeoplePickerValueForEmail(web, email); // Share with the people picker value return(ShareSiteWithPeoplePickerValue(web, peoplePickerValue, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
protected void btnShareSite_Click(object sender, EventArgs e) { if (txtTargetEmail.Text.Length > 0) { var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var ctx = spContext.CreateUserClientContextForSPHost()) { // Check if this is edit link or not ExternalSharingSiteOption shareType = SolveSelectedShareType(); // Share a site for the given email address SharingResult result = ctx.Web.ShareSite(txtTargetEmail.Text, shareType, true, "Here's a site shared for you."); // Output the created link lblStatus.Text = string.Format("Site sharing status: {0}", result.StatusCode.ToString()); } } else { lblStatus.Text = "Please assign the email to target the site to."; } }
/// <summary> /// Share site for a person using complex JSON object for people picker value. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="peoplePickerInput">JSON object with the people picker value</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSiteWithPeoplePickerValue(this Web web, string peoplePickerInput, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve the group id for the shared option based on default groups var groupId = SolveGroupIdToShare(web, shareOption); string roleValue = $"group:{groupId}"; // Right permission setup web.EnsureProperty(w => w.Url); // Set default settings for site sharing var propagateAcl = false; // Not relevant for external accounts var includedAnonymousLinkInEmail = false; // Not when site is shared var result = Web.ShareObject(web.Context, web.Url, peoplePickerInput, roleValue, 0, propagateAcl, sendEmail, includedAnonymousLinkInEmail, null, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return(result); }
private static async Task <SharingResult> ShareSiteImplementation(Web web, string email, ExternalSharingSiteOption shareOption, bool sendEmail, string emailBody, bool useSimplifiedRoles) { // Solve people picker value for email address string peoplePickerValue = await ResolvePeoplePickerValueForEmailImplementation(web, email); // Share with the people picker value return(await ShareSiteWithPeoplePickerValueImplementation(web, peoplePickerValue, shareOption, sendEmail, emailBody, useSimplifiedRoles)); }
/// <summary> /// Used to solve right group ID to assign user into - used for the site level sharing. /// </summary> /// <param name="web">Web to be shared externally</param> /// <param name="shareOption">Permissions to be given for the external user</param> /// <returns></returns> private static int SolveGroupIdToShare(Web web, ExternalSharingSiteOption shareOption) { Group group = null; switch (shareOption) { case ExternalSharingSiteOption.Owner: group = web.AssociatedOwnerGroup; break; case ExternalSharingSiteOption.Edit: group = web.AssociatedMemberGroup; break; case ExternalSharingSiteOption.View: group = web.AssociatedVisitorGroup; break; default: group = web.AssociatedVisitorGroup; break; } // Load right group web.Context.Load(group); web.Context.ExecuteQueryRetry(); // Return group ID return group.Id; }
/// <summary> /// Share site for a person using complex JSON object for people picker value. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="peoplePickerInput">JSON object with the people picker value</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSiteWithPeoplePickerValue(this Web web, string peoplePickerInput, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve the group id for the shared option based on default groups int groupId = SolveGroupIdToShare(web, shareOption); string roleValue = string.Format("group:{0}", groupId); // Right permission setup // Ensure that web URL has been loaded if (!web.IsObjectPropertyInstantiated("Url")) { web.Context.Load(web, w => w.Url); web.Context.ExecuteQueryRetry(); } // Set default settings for site sharing bool propageAcl = false; // Not relevant for external accounts bool includedAnonymousLinkInEmail = false; // Not when site is shared SharingResult result = Microsoft.SharePoint.Client.Web.ShareObject(web.Context, web.Url, peoplePickerInput, roleValue, 0, propageAcl, sendEmail, includedAnonymousLinkInEmail, null, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return result; }
/// <summary> /// Share site for a person using just email. Will resolve needed people picker JSON value automatically. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="email">Email of the person to whom site should be shared.</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSite(this Web web, string email, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve people picker value for email address string peoplePickerValue = ResolvePeoplePickerValueForEmail(web, email); // Share with the people picker value return ShareSiteWithPeoplePickerValue(web, peoplePickerValue, shareOption, sendEmail, emailBody, useSimplifiedRoles); }
/// <summary> /// Share site for a person using complex JSON object for people picker value. /// </summary> /// <param name="web">Web for the context of the site to be shared.</param> /// <param name="peoplePickerInput">JSON object with the people picker value</param> /// <param name="shareOption">Sharing style - View, Edit, Owner</param> /// <param name="sendEmail">Should we send email for the given address.</param> /// <param name="emailBody">Text to be added on share email sent to receiver.</param> /// <param name="useSimplifiedRoles">Boolean value indicating whether to use the SharePoint simplified roles (Edit, View)</param> /// <returns></returns> public static SharingResult ShareSiteWithPeoplePickerValue(this Web web, string peoplePickerInput, ExternalSharingSiteOption shareOption, bool sendEmail = true, string emailBody = "Site shared for you.", bool useSimplifiedRoles = true) { // Solve the group id for the shared option based on default groups var groupId = SolveGroupIdToShare(web, shareOption); string roleValue = $"group:{groupId}"; // Right permission setup web.EnsureProperty(w => w.Url); // Set default settings for site sharing var propagateAcl = false; // Not relevant for external accounts var includedAnonymousLinkInEmail = false; // Not when site is shared var result = Web.ShareObject(web.Context, web.Url, peoplePickerInput, roleValue, 0, propagateAcl, sendEmail, includedAnonymousLinkInEmail, null, emailBody, useSimplifiedRoles); web.Context.Load(result); web.Context.ExecuteQueryRetry(); return result; }