public async Task HandleExceptions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = false; using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite)) { Assert.ThrowsException <ArgumentNullException>(() => { SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = false }; using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(null, siteCreationOptions)) { } }); Assert.ThrowsException <ArgumentNullException>(() => { CommunicationSiteOptions communicationSiteToCreate = new CommunicationSiteOptions(null, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, }; }); Assert.ThrowsException <ArgumentNullException>(() => { CommunicationSiteOptions communicationSiteToCreate = new CommunicationSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), null) { Description = "This is a test site collection", Language = Language.English, }; }); Assert.ThrowsException <ArgumentNullException>(() => { TeamSiteOptions communicationSiteToCreate = new TeamSiteOptions(null, "display name"); }); Assert.ThrowsException <ArgumentNullException>(() => { TeamSiteOptions communicationSiteToCreate = new TeamSiteOptions("alias", null); }); Assert.ThrowsException <ArgumentNullException>(() => { ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(null, "title", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None); }); Assert.ThrowsException <ArgumentNullException>(() => { ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None); }); Assert.ThrowsException <ArgumentNullException>(() => { ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "", "owner", Language.Default, Model.SharePoint.TimeZone.None); }); Assert.ThrowsException <ArgumentNullException>(() => { ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "webtemplate", "", Language.Default, Model.SharePoint.TimeZone.None); }); Assert.ThrowsException <ArgumentException>(() => { ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None); }); Assert.ThrowsException <ArgumentNullException>(() => { CreationOptions creationOptions = new CreationOptions() { UsingApplicationPermissions = false }; context.GetSiteCollectionManager().ConnectSiteCollectionToGroup(null, creationOptions); }); Assert.ThrowsException <ArgumentNullException>(() => { ConnectSiteToGroupOptions communicationSiteToCreate = new ConnectSiteToGroupOptions(null, "alias", "displayname"); }); Assert.ThrowsException <ArgumentNullException>(() => { context.GetSiteCollectionManager().RecycleSiteCollection(null); }); Assert.ThrowsException <ArgumentNullException>(() => { context.GetSiteCollectionManager().RestoreSiteCollection(null); }); Assert.ThrowsException <ArgumentNullException>(() => { context.GetSiteCollectionManager().DeleteSiteCollection(null); }); Assert.ThrowsException <ArgumentNullException>(() => { context.GetSiteCollectionManager().GetSiteCollectionProperties(null); }); } }
public async Task <HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req) { logger.LogInformation("CreateSite function starting..."); // Parse the url parameters NameValueCollection parameters = HttpUtility.ParseQueryString(req.Url.Query); var siteName = parameters["siteName"]; var owner = parameters["owner"]; HttpResponseData response = null; try { using (var pnpContext = await contextFactory.CreateAsync("Default")) { response = req.CreateResponse(HttpStatusCode.OK); response.Headers.Add("Content-Type", "application/json"); var communicationSiteToCreate = new CommunicationSiteOptions(new Uri($"https://{pnpContext.Uri.DnsSafeHost}/sites/{siteName}"), "Demo site") { Description = "PnP Core SDK demo site", Language = Language.English, Owner = $"i:0#.f|membership|{owner}" }; logger.LogInformation($"Creating site: {communicationSiteToCreate.Url}"); // Create the new site collection using (var newSiteContext = await pnpContext.GetSiteCollectionManager().CreateSiteCollectionAsync(communicationSiteToCreate)) { logger.LogInformation($"Site created: {communicationSiteToCreate.Url}"); // Step 1: Upload image to site assets library var siteAssetsLibrary = await newSiteContext.Web.Lists.EnsureSiteAssetsLibraryAsync(p => p.RootFolder); var uploadFolder = await siteAssetsLibrary.RootFolder.EnsureFolderAsync("SitePages/PnP"); var addedFile = await uploadFolder.Files.AddAsync("parker.png", File.OpenRead($".{Path.DirectorySeparatorChar}parker.png"), true); // Step 2: Create the page var page = await newSiteContext.Web.NewPageAsync(); page.AddSection(CanvasSectionTemplate.OneColumn, 1); // Add text with inline image var text = page.NewTextPart(); var parker = await page.GetInlineImageAsync(text, addedFile.ServerRelativeUrl, new PageImageOptions { Alignment = PageImageAlignment.Left }); text.Text = $"<H2>Hello everyone!</H2>{parker}<P>Community rocks, sharing is caring!</P>"; page.AddControl(text, page.Sections[0].Columns[0]); // Save the page await page.SaveAsync("PnP.aspx"); // Return the URL of the created site await response.WriteStringAsync(JsonSerializer.Serialize(new { siteUrl = newSiteContext.Uri.AbsoluteUri })); } return(response); } } catch (Exception ex) { response = req.CreateResponse(HttpStatusCode.OK); response.Headers.Add("Content-Type", "application/json"); await response.WriteStringAsync(JsonSerializer.Serialize(new { error = ex.Message })); return(response); } }
public async Task CreateRestoreDeleteUsingDelegatedPermissions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = false; CommunicationSiteOptions communicationSiteToCreate = null; // Create the site collection try { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite)) { using (var adminContext = await context.GetSharePointAdmin().GetTenantAdminCenterContextAsync()) { // Persist the used site url as we need to have the same url when we run an offline test Uri siteUrl; if (!TestCommon.Instance.Mocking) { siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestcommsite{Guid.NewGuid().ToString().Replace("-", "")}"); Dictionary <string, string> properties = new Dictionary <string, string> { { "SiteUrl", siteUrl.ToString() } }; TestManager.SaveProperties(context, properties); } else { siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]); } communicationSiteToCreate = new CommunicationSiteOptions(siteUrl, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, }; SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = false }; using (var newSiteContext = adminContext.GetSiteCollectionManager().CreateSiteCollection(communicationSiteToCreate, siteCreationOptions)) { var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language); Assert.IsTrue(web.Url == communicationSiteToCreate.Url); Assert.IsTrue(web.Title == communicationSiteToCreate.Title); Assert.IsTrue(web.Description == communicationSiteToCreate.Description); Assert.IsTrue(web.Language == (int)communicationSiteToCreate.Language); } if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(30)); } // Recycle the site collection adminContext.GetSiteCollectionManager().RecycleSiteCollection(communicationSiteToCreate.Url); if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(30)); } // Verify the site collection is returned as recycled site var recycledSites = adminContext.GetSiteCollectionManager().GetRecycledSiteCollections(); var recycledCommunicationSite = recycledSites.FirstOrDefault(c => c.Url == communicationSiteToCreate.Url); Assert.IsNotNull(recycledCommunicationSite); Assert.IsTrue(!string.IsNullOrEmpty(recycledCommunicationSite.Name)); Assert.IsTrue(!string.IsNullOrEmpty(recycledCommunicationSite.CreatedBy)); Assert.IsTrue(!string.IsNullOrEmpty(recycledCommunicationSite.DeletedBy)); Assert.IsTrue(recycledCommunicationSite.TimeCreated > DateTime.MinValue); Assert.IsTrue(recycledCommunicationSite.TimeDeleted > DateTime.MinValue); Assert.IsTrue(recycledCommunicationSite.StorageQuota > 0); Assert.IsTrue(recycledCommunicationSite.StorageUsed > 0); Assert.IsTrue(!string.IsNullOrEmpty(recycledCommunicationSite.TemplateName)); // Restore the recycled site collection again adminContext.GetSiteCollectionManager().RestoreSiteCollection(communicationSiteToCreate.Url); if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(30)); } // Verify the site collection is not returned as recycled site recycledSites = adminContext.GetSiteCollectionManager().GetRecycledSiteCollections(); recycledCommunicationSite = recycledSites.FirstOrDefault(c => c.Url == communicationSiteToCreate.Url); Assert.IsNull(recycledCommunicationSite); if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(30)); } } } } finally { // Clean up the created site collection TestCommon.Instance.UseApplicationPermissions = false; using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1)) { context.GetSiteCollectionManager().DeleteSiteCollection(communicationSiteToCreate.Url); } } }
public async Task CreateCommunicationSiteUsingDelegatedPermissions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = false; CommunicationSiteOptions communicationSiteToCreate = null; // Create the site collection try { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite)) { // Persist the used site url as we need to have the same url when we run an offline test Uri siteUrl; if (!TestCommon.Instance.Mocking) { siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestcommsite{Guid.NewGuid().ToString().Replace("-", "")}"); Dictionary <string, string> properties = new Dictionary <string, string> { { "SiteUrl", siteUrl.ToString() } }; TestManager.SaveProperties(context, properties); } else { siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]); } communicationSiteToCreate = new CommunicationSiteOptions(siteUrl, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, }; SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = false }; int rewrites = 0; context.BatchClient.MockingFileRewriteHandler = (string input) => { if (rewrites < 2 && input.Contains(",\"SiteStatus\":2,")) { input = input.Replace(",\"SiteStatus\":2,", ",\"SiteStatus\":1,"); rewrites++; } return(input); }; using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(communicationSiteToCreate, siteCreationOptions)) { var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language); Assert.IsTrue(web.Url == communicationSiteToCreate.Url); Assert.IsTrue(web.Title == communicationSiteToCreate.Title); Assert.IsTrue(web.Description == communicationSiteToCreate.Description); Assert.IsTrue(web.Language == (int)communicationSiteToCreate.Language); } if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(15)); } } } finally { TestCommon.Instance.UseApplicationPermissions = false; using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1)) { context.GetSiteCollectionManager().DeleteSiteCollection(communicationSiteToCreate.Url); } } }
public async Task CreateCommunicationSiteUsingApplicationPermissions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = true; CommunicationSiteOptions communicationSiteToCreate = null; // Create the site collection try { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.NoGroupTestSite)) { // Determine the user to set as owner await context.Web.LoadAsync(p => p.AssociatedOwnerGroup.QueryProperties(p => p.Users)); var user = context.Web.AssociatedOwnerGroup.Users.AsRequested().FirstOrDefault(); // Persist the used site url as we need to have the same url when we run an offline test Uri siteUrl; string owner = user.LoginName; if (!TestCommon.Instance.Mocking) { siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestcommsite{Guid.NewGuid().ToString().Replace("-", "")}"); Dictionary <string, string> properties = new Dictionary <string, string> { { "SiteUrl", siteUrl.ToString() }, { "SiteOwner", owner } }; TestManager.SaveProperties(context, properties); } else { siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]); owner = TestManager.GetProperties(context)["SiteOwner"]; } communicationSiteToCreate = new CommunicationSiteOptions(siteUrl, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, Owner = owner }; SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = true }; using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(communicationSiteToCreate, siteCreationOptions)) { var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language); Assert.IsTrue(web.Url == communicationSiteToCreate.Url); Assert.IsTrue(web.Title == communicationSiteToCreate.Title); Assert.IsTrue(web.Description == communicationSiteToCreate.Description); Assert.IsTrue(web.Language == (int)communicationSiteToCreate.Language); } if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(15)); } } } finally { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1)) { context.GetSiteCollectionManager().DeleteSiteCollection(communicationSiteToCreate.Url); } TestCommon.Instance.UseApplicationPermissions = false; } }
public async Task CreateCommunicationSiteAdvancedUsingDelegatedPermissions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = false; CommunicationSiteOptions communicationSiteToCreate = null; // Create the site collection try { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite)) { // Get a list of available sensitivity labels var labels = await SensitivityLabelManager.GetLabelsUsingDelegatedPermissionsAsync(context); var siteLabel = labels.FirstOrDefault(); Guid sensitivityLabelId = Guid.Empty; // Persist the used site url as we need to have the same url when we run an offline test Uri siteUrl; if (!TestCommon.Instance.Mocking) { siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestcommsite{Guid.NewGuid().ToString().Replace("-", "")}"); if (siteLabel != null) { sensitivityLabelId = siteLabel.Id; } Dictionary <string, string> properties = new Dictionary <string, string> { { "SiteUrl", siteUrl.ToString() }, { "SensitivityLabelId", sensitivityLabelId.ToString() } }; TestManager.SaveProperties(context, properties); } else { siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]); sensitivityLabelId = Guid.Parse(TestManager.GetProperties(context)["SensitivityLabelId"]); } communicationSiteToCreate = new CommunicationSiteOptions(siteUrl, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, SensitivityLabelId = sensitivityLabelId, ShareByEmailEnabled = true, }; SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = false }; using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(communicationSiteToCreate, siteCreationOptions)) { var site = await newSiteContext.Site.GetAsync(p => p.SensitivityLabelId, p => p.SensitivityLabel); var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language); Assert.IsTrue(web.Url == communicationSiteToCreate.Url); Assert.IsTrue(web.Title == communicationSiteToCreate.Title); Assert.IsTrue(web.Description == communicationSiteToCreate.Description); Assert.IsTrue(web.Language == (int)communicationSiteToCreate.Language); Assert.IsTrue(site.SensitivityLabelId == communicationSiteToCreate.SensitivityLabelId); } if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(15)); } } } finally { TestCommon.Instance.UseApplicationPermissions = false; using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1)) { context.GetSiteCollectionManager().DeleteSiteCollection(communicationSiteToCreate.Url); } } }
public async Task SetSiteCollectionAdminsRegularSiteApplicationPermissions() { //TestCommon.Instance.Mocking = false; TestCommon.Instance.UseApplicationPermissions = true; CommunicationSiteOptions communicationSiteToCreate = null; // Create the site collection try { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.NoGroupTestSite)) { // Determine the user to set as owner await context.Web.LoadAsync(p => p.AssociatedOwnerGroup.QueryProperties(p => p.Users)); var user = context.Web.AssociatedOwnerGroup.Users.AsRequested().FirstOrDefault(); // Persist the used site url as we need to have the same url when we run an offline test Uri siteUrl; string owner = user.LoginName; if (!TestCommon.Instance.Mocking) { siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestcommsite{Guid.NewGuid().ToString().Replace("-", "")}"); Dictionary <string, string> properties = new Dictionary <string, string> { { "SiteUrl", siteUrl.ToString() }, { "SiteOwner", owner } }; TestManager.SaveProperties(context, properties); } else { siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]); owner = TestManager.GetProperties(context)["SiteOwner"]; } communicationSiteToCreate = new CommunicationSiteOptions(siteUrl, "PnP Core SDK Test") { Description = "This is a test site collection", Language = Language.English, Owner = owner }; SiteCreationOptions siteCreationOptions = new SiteCreationOptions() { UsingApplicationPermissions = true }; context.GetSiteCollectionManager().CreateSiteCollection(communicationSiteToCreate, siteCreationOptions); // get current admins var admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(communicationSiteToCreate.Url); // update admins List <string> newAdmins = new List <string>(); foreach (var admin in admins) { newAdmins.Add(admin.LoginName); } // everyone claim newAdmins.Add("c:0(.s|true"); // set admins context.GetSiteCollectionManager().SetSiteCollectionAdmins(communicationSiteToCreate.Url, newAdmins); // Get admins again and verify if the added admin is present admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(communicationSiteToCreate.Url); Assert.IsNotNull(admins.FirstOrDefault(p => p.LoginName == "c:0(.s|true")); if (context.Mode == TestMode.Record) { // Add a little delay between creation and deletion await Task.Delay(TimeSpan.FromSeconds(15)); } } } finally { using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1)) { context.GetSiteCollectionManager().DeleteSiteCollection(communicationSiteToCreate.Url); } TestCommon.Instance.UseApplicationPermissions = false; } }