Beispiel #1
0
        /// <summary>
        /// Gets a list of the available site policies
        /// </summary>
        /// <param name="web">Web to operate on</param>
        /// <returns>A list of <see cref="SitePolicyEntity"/> objects</returns>
        public static List <SitePolicyEntity> GetSitePolicies(this Web web)
        {
            ClientObjectList <ProjectPolicy> sitePolicies = ProjectPolicy.GetProjectPolicies(web.Context, web);

            web.Context.Load(sitePolicies);
            web.Context.ExecuteQueryRetry();

            List <SitePolicyEntity> policies = new List <SitePolicyEntity>();

            if (sitePolicies != null && sitePolicies.Count > 0)
            {
                foreach (var policy in sitePolicies)
                {
                    policies.Add(new SitePolicyEntity
                    {
                        Name        = policy.Name,
                        Description = policy.Description,
                        EmailBody   = policy.EmailBody,
                        EmailBodyWithTeamMailbox = policy.EmailBodyWithTeamMailbox,
                        EmailSubject             = policy.EmailSubject
                    });
                }
            }

            return(policies);
        }
Beispiel #2
0
        private static async Task <List <SitePolicyEntity> > GetSitePoliciesImplementation(this Web web)
#endif
        {
            var sitePolicies = ProjectPolicy.GetProjectPolicies(web.Context, web);

            web.Context.Load(sitePolicies);
#if ONPREMISES
            web.Context.ExecuteQueryRetry();
#else
            await web.Context.ExecuteQueryRetryAsync();
#endif

            var policies = new List <SitePolicyEntity>();

            if (sitePolicies != null && sitePolicies.Count > 0)
            {
                foreach (var policy in sitePolicies)
                {
                    policies.Add(new SitePolicyEntity
                    {
                        Name        = policy.Name,
                        Description = policy.Description,
                        EmailBody   = policy.EmailBody,
                        EmailBodyWithTeamMailbox = policy.EmailBodyWithTeamMailbox,
                        EmailSubject             = policy.EmailSubject
                    });
                }
            }

            return(policies);
        }
Beispiel #3
0
        private static async Task <bool> ApplySitePolicyImplementation(this Web web, string sitePolicy)
#endif
        {
            var result = false;

            var sitePolicies = ProjectPolicy.GetProjectPolicies(web.Context, web);

            web.Context.Load(sitePolicies);
#if ONPREMISES
            web.Context.ExecuteQueryRetry();
#else
            await web.Context.ExecuteQueryRetryAsync();
#endif

            if (sitePolicies != null && sitePolicies.Count > 0)
            {
                var policyToApply = sitePolicies.FirstOrDefault(p => p.Name == sitePolicy);

                if (policyToApply != null)
                {
                    ProjectPolicy.ApplyProjectPolicy(web.Context, web, policyToApply);
#if ONPREMISES
                    web.Context.ExecuteQueryRetry();
#else
                    await web.Context.ExecuteQueryRetryAsync();
#endif
                    result = true;
                }
            }

            return(result);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string      json    = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sposettings.json");
            SPOSettings setting = SPOSettings.GetSettings(json);

            using (ClientContext context = GetClientContext(setting))
            {
                Web web          = context.Web;
                var sitePolicies = ProjectPolicy.GetProjectPolicies(context, web);
                context.Load(sitePolicies);
                context.ExecuteQuery();


                if (sitePolicies != null && sitePolicies.Count > 0)
                {
                    var policy = sitePolicies.FirstOrDefault(p => p.Name == setting.PolicyName);
                    if (policy != null)
                    {
                        ProjectPolicy.ApplyProjectPolicy(context, web, policy);
                        context.ExecuteQuery();

                        ProjectPolicy.CloseProject(context, web);
                        context.ExecuteQuery();
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Apply a policy to a site
        /// </summary>
        /// <param name="web">Web to operate on</param>
        /// <param name="sitePolicy">Policy to apply</param>
        /// <returns>True if applied, false otherwise</returns>
        public static bool ApplySitePolicy(this Web web, string sitePolicy)
        {
            bool result = false;

            ClientObjectList <ProjectPolicy> sitePolicies = ProjectPolicy.GetProjectPolicies(web.Context, web);

            web.Context.Load(sitePolicies);
            web.Context.ExecuteQueryRetry();

            if (sitePolicies != null && sitePolicies.Count > 0)
            {
                ProjectPolicy policyToApply = sitePolicies.FirstOrDefault(p => p.Name == sitePolicy);

                if (policyToApply != null)
                {
                    ProjectPolicy.ApplyProjectPolicy(web.Context, web, policyToApply);
                    web.Context.ExecuteQueryRetry();
                    result = true;
                }
            }

            return(result);
        }