/// <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 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); }
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> /// Can be used to programatically to unshare any document with the document URL. /// </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> public static SharingResult UnshareDocument(this Web web, string urlToDocument) { SharingResult result = Microsoft.SharePoint.Client.Web.UnshareObject(web.Context, urlToDocument); web.Context.Load(result); web.Context.ExecuteQueryRetry(); // Return the results return(result); }
public void NativeShare_OnShareCompleted(string result) { SharingResult r = (SharingResult)System.Int32.Parse(result); if (OnShareCompleted != null) { OnShareCompleted(r); } if (listenner != null) { DestroyObject(gameObject); } }
protected void btnUnShareDoc_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()) { // Get file link URL string fileUrl = ResolveShareUrl(ctx, documents.SelectedValue); // Unshare the document - remove all external shares SharingResult result = ctx.Web.UnshareDocument(fileUrl); // Output the created link lblStatus.Text = string.Format("Document unsharing status: {0}", result.StatusCode); } } else { lblStatus.Text = "No document selected to be unshared."; } }
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> /// This method will send notifications to external users /// </summary> /// <param name="externalSharingRequest"></param> /// <returns></returns> private GenericResponseVM SendExternalNotification(MatterInformationVM matterInformation, string permission, string externalEmail) { var clientUrl = $"{matterInformation.Client.Url}"; try { string roleId = ""; switch (permission.ToLower()) { case "full control": roleId = ((int)SPORoleIdMapping.FullControl).ToString(); break; case "contribute": roleId = ((int)SPORoleIdMapping.Contribute).ToString(); break; case "read": roleId = ((int)SPORoleIdMapping.Read).ToString(); break; } PeoplePickerUser peoplePickerUser = new PeoplePickerUser(); peoplePickerUser.Key = externalEmail; peoplePickerUser.Description = externalEmail; peoplePickerUser.DisplayText = externalEmail; peoplePickerUser.EntityType = ""; peoplePickerUser.ProviderDisplayName = ""; peoplePickerUser.ProviderName = ""; peoplePickerUser.IsResolved = true; EntityData entityData = new EntityData(); entityData.SPUserID = externalEmail; entityData.Email = externalEmail; entityData.IsBlocked = "False"; entityData.PrincipalType = "UNVALIDATED_EMAIL_ADDRESS"; entityData.AccountName = externalEmail; entityData.SIPAddress = externalEmail; peoplePickerUser.EntityData = entityData; string peoplePicker = Newtonsoft.Json.JsonConvert.SerializeObject(peoplePickerUser); peoplePicker = $"[{peoplePicker}]"; string roleValue = $"role:{roleId}"; bool sendEmail = true; bool includeAnonymousLinkInEmail = false; bool propagateAcl = false; bool useSimplifiedRoles = true; string matterLandingPageUrl = $"{clientUrl}/sitepages/{matterInformation.Matter.MatterGuid + ServiceConstants.ASPX_EXTENSION}"; string url = matterLandingPageUrl; string emailBody = $"The following information has been shared with you {matterInformation.Matter.Name}"; string emailSubject = $"The following information has been shared with you {matterInformation.Matter.Name}"; #region Doc Sharing API SharingResult sharingResult = null; using (var clientContext = spoAuthorization.GetClientContext(clientUrl)) { sharingResult = Microsoft.SharePoint.Client.Web.ShareObject(clientContext, url, peoplePicker, roleValue, 0, propagateAcl, sendEmail, includeAnonymousLinkInEmail, emailSubject, emailBody, useSimplifiedRoles); clientContext.Load(sharingResult); clientContext.ExecuteQuery(); } return(null); #endregion } catch (Exception ex) { throw; } }