Пример #1
0
        public async Task CreateTeamSiteWithoutGroupUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteWithoutGroupOptions teamSiteToCreate = 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/pnpcoresdktestteamsite{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"]);
                    }

                    teamSiteToCreate = new TeamSiteWithoutGroupOptions(siteUrl, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language);

                        Assert.IsTrue(web.Url == teamSiteToCreate.Url);
                        Assert.IsTrue(web.Title == teamSiteToCreate.Title);
                        Assert.IsTrue(web.Description == teamSiteToCreate.Description);
                        Assert.IsTrue(web.Language == (int)teamSiteToCreate.Language);
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(teamSiteToCreate.Url);
                }
            }
        }
Пример #2
0
        public async Task EnableCommunicationSiteFeaturesOnClassicSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            ClassicSiteOptions classicSite = null;

            // Create the site collection
            try
            {
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
                {
                    using (var tenantAdminContext = 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;
                        string owner;
                        if (!TestCommon.Instance.Mocking)
                        {
                            siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestclassicsite{Guid.NewGuid().ToString().Replace("-", "")}");
                            owner   = context.Web.GetCurrentUser().LoginName;
                            Dictionary <string, string> properties = new Dictionary <string, string>
                            {
                                { "SiteUrl", siteUrl.ToString() },
                                { "Owner", owner }
                            };
                            TestManager.SaveProperties(context, properties);
                        }
                        else
                        {
                            siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]);
                            owner   = TestManager.GetProperties(context)["Owner"];
                        }

                        classicSite = new ClassicSiteOptions(siteUrl, "PnP Core SDK Test", "STS#0", owner, Language.English,
                                                             Model.SharePoint.TimeZone.UTCPLUS0100_BRUSSELS_COPENHAGEN_MADRID_PARIS);

                        SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                        {
                            UsingApplicationPermissions = false
                        };

                        using (var newSiteContext = tenantAdminContext.GetSiteCollectionManager().CreateSiteCollection(classicSite, siteCreationOptions))
                        {
                            var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language);

                            Assert.IsTrue(web.Url == classicSite.Url);
                            Assert.IsTrue(web.Title == classicSite.Title);
                            Assert.IsTrue(web.Language == (int)classicSite.Language);

                            Assert.ThrowsException <ClientException>(() => newSiteContext.GetSiteCollectionManager().EnableCommunicationSiteFeatures(newSiteContext.Uri, Guid.Empty));
                            Assert.ThrowsException <ClientException>(() => newSiteContext.GetSiteCollectionManager().EnableCommunicationSiteFeatures(newSiteContext.Uri, Guid.NewGuid()));

                            // Enable the communication site features on this classic site
                            newSiteContext.GetSiteCollectionManager().EnableCommunicationSiteFeatures(newSiteContext.Uri);

                            // create a full width page...as that only works on a site with the communication features active. This also verifies that modern page creation is enabled
                            var page = await newSiteContext.Web.NewPageAsync();

                            string pageName = TestCommon.GetPnPSdkTestAssetName("fullwidth.aspx");

                            // Add all the possible sections
                            page.AddSection(CanvasSectionTemplate.OneColumnFullWidth, 1);

                            // Instantiate a default web part
                            var imageWebPartComponent = await page.InstantiateDefaultWebPartAsync(DefaultWebPart.Image);

                            // Add a text control in each section
                            page.AddControl(imageWebPartComponent, page.Sections[0].Columns[0]);

                            await page.SaveAsync(pageName);

                            // load page again
                            var pages = await newSiteContext.Web.GetPagesAsync(pageName);

                            Assert.IsTrue(pages.Count == 1);

                            page = pages.AsEnumerable().First();

                            Assert.IsTrue(page.Sections.Count == 1);
                            Assert.IsTrue(page.Sections[0].Type == CanvasSectionTemplate.OneColumnFullWidth);
                            Assert.IsTrue(page.Sections[0].Columns[0].Controls.Count == 1);
                            Assert.IsTrue(page.Sections[0].Columns[0].Controls[0] is IPageWebPart);
                            Assert.IsTrue((page.Sections[0].Columns[0].Controls[0] as IPageWebPart).WebPartId == page.DefaultWebPartToWebPartId(DefaultWebPart.Image));
                        }
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(classicSite.Url);
                }
            }
        }
Пример #3
0
        public async Task TeamifyTeamSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            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
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        createdSiteCollection = newSiteContext.Uri;

                        Assert.IsTrue(newSiteContext.Site.GroupId != Guid.Empty);

                        // Check prompt
                        var isAddTeamsPromptHidden = newSiteContext.GetSiteCollectionManager().IsAddTeamsPromptHidden(newSiteContext.Uri);

                        Assert.IsFalse(isAddTeamsPromptHidden);

                        // Hide prompt
                        var hidden = newSiteContext.GetSiteCollectionManager().HideAddTeamsPrompt(newSiteContext.Uri);

                        isAddTeamsPromptHidden = newSiteContext.GetSiteCollectionManager().IsAddTeamsPromptHidden(newSiteContext.Uri);
                        Assert.IsTrue(isAddTeamsPromptHidden);

                        // Add teams team
                        using (var contextWithTeam = newSiteContext.GetTeamManager().CreateTeam(new TeamForGroupOptions(newSiteContext.Site.GroupId)))
                        {
                            Assert.IsTrue(contextWithTeam.Team.Requested);
                            Assert.IsTrue(contextWithTeam.Team.IsPropertyAvailable(p => p.Id));
                        }
                    }

                    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(createdSiteCollection);
                }
            }
        }
Пример #4
0
        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);
                });
            }
        }
Пример #5
0
        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);
                }
            }
        }
Пример #6
0
        public async Task CreateClassicSiteUsingApplicationPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = true;

            ClassicSiteOptions classicSite = null;

            // Create the site collection
            try
            {
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.NoGroupTestSite))
                {
                    using (var tenantAdminContext = await context.GetSharePointAdmin().GetTenantAdminCenterContextAsync())
                    {
                        // 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/pnpcoresdktestclassicsite{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"];
                        }

                        classicSite = new ClassicSiteOptions(siteUrl, "PnP Core SDK Test", "STS#0", owner, Language.English,
                                                             Model.SharePoint.TimeZone.UTCPLUS0100_BRUSSELS_COPENHAGEN_MADRID_PARIS);

                        SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                        {
                            UsingApplicationPermissions = false
                        };

                        using (var newSiteContext = tenantAdminContext.GetSiteCollectionManager().CreateSiteCollection(classicSite, siteCreationOptions))
                        {
                            var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title, p => p.Description, p => p.Language);

                            Assert.IsTrue(web.Url == classicSite.Url);
                            Assert.IsTrue(web.Title == classicSite.Title);
                            Assert.IsTrue(web.Language == (int)classicSite.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(classicSite.Url);
                }
            }
        }
Пример #7
0
        public async Task CreateTeamSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            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
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    int rewrites = 0;
                    context.BatchClient.MockingFileRewriteHandler = (string input) =>
                    {
                        if (!siteCreationOptions.UsingApplicationPermissions.Value && rewrites < 2 && input.Contains(",\"SiteStatus\":2,"))
                        {
                            input = input.Replace(",\"SiteStatus\":2,", ",\"SiteStatus\":1,");
                            rewrites++;
                        }
                        return(input);
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        createdSiteCollection = newSiteContext.Uri;

                        Assert.IsTrue(newSiteContext.Site.GroupId != Guid.Empty);

                        var web = await newSiteContext.Web.GetAsync(p => p.Title, p => p.Description, p => p.Language);

                        Assert.IsTrue(web.Description == teamSiteToCreate.Description);
                        Assert.IsTrue(web.Language == (int)teamSiteToCreate.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(createdSiteCollection);
                }
            }
        }
Пример #8
0
        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);
                }
            }
        }
Пример #9
0
        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);
                }
            }
        }
Пример #10
0
        public async Task SetSiteCollectionAdminsGroupSite()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            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
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions);
                    createdSiteCollection = newSiteContext.Uri;

                    // get current admins
                    var admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(createdSiteCollection);

                    // 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");

                    //Also set admin via group owner membership
                    List <Guid> newGroupOwners = new List <Guid>();
                    foreach (var groupOwner in admins.Where(p => p.IsMicrosoft365GroupOwner == true))
                    {
                        newGroupOwners.Add(groupOwner.Id);
                    }

                    // set admins
                    context.GetSiteCollectionManager().SetSiteCollectionAdmins(createdSiteCollection, newAdmins, newGroupOwners);

                    // Get admins again and verify if the added admin is present
                    admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(createdSiteCollection);

                    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
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(createdSiteCollection);
                }
            }
        }
Пример #11
0
        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;
            }
        }
Пример #12
0
        public async Task ConnectGroupToExistingSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteWithoutGroupOptions teamSiteToCreate = null;

            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;
                    string alias = null;
                    if (!TestCommon.Instance.Mocking)
                    {
                        Guid tempId = Guid.NewGuid();
                        siteUrl = new Uri($"https://{context.Uri.DnsSafeHost}/sites/pnpcoresdktestteamsite{tempId.ToString().Replace("-", "")}");
                        alias   = $"pnpcoresdktestteamsite{tempId.ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "SiteUrl", siteUrl.ToString() },
                            { "Alias", alias },
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        siteUrl = new Uri(TestManager.GetProperties(context)["SiteUrl"]);
                        alias   = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteWithoutGroupOptions(siteUrl, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    // first create the site collection
                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        var web = await newSiteContext.Web.GetAsync(p => p.Url, p => p.Title);

                        // Add a group to the created site collection
                        ConnectSiteToGroupOptions groupConnectOptions = new ConnectSiteToGroupOptions(teamSiteToCreate.Url, alias, web.Title);
                        CreationOptions           creationOptions     = new CreationOptions()
                        {
                            UsingApplicationPermissions = false
                        };
                        newSiteContext.GetSiteCollectionManager().ConnectSiteCollectionToGroup(groupConnectOptions, creationOptions);

                        // load site again to see if there's a group connected
                        await context.Site.LoadAsync(p => p.GroupId);

                        Assert.IsTrue(context.Site.GroupId != Guid.Empty);
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                // Cleanup the created site collection
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(teamSiteToCreate.Url);
                }
            }
        }