コード例 #1
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);
                });
            }
        }
コード例 #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 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);
                }
            }
        }