1

I have a custom contenttype created in VS. I would like to set a policy label on this contenttype. How can I do this programmatically?

enter image description here

0

You can add contenttype label policies as follow:

public static void AddPolicyToContentType(SPSite site)
        {
            SPContentType ct = site.RootWeb.ContentTypes[new SPContentTypeId("0x0101000ACB09B019144AB4A7A9EDD1CCCC4ED3")];

            const string PolicyName = "DMS Version Policy";
            // Policy XML for enabling all out of the box auditing
            const string LabelCustomData = "<label><segment type=\"metadata\">_UIVersionString</segment></label>";

            Policy policy = Policy.GetPolicy(ct);
            if (policy == null)
            {
                // Create new custom policy
                Policy.CreatePolicy(ct, null);
                // Retrieve newly created policy
                policy = Policy.GetPolicy(ct);

                if (policy == null)
                {
                    throw new NullReferenceException("policy");
                }

                // Update policy information
                policy.Statement = PolicyName;
                policy.Name = PolicyName;
                policy.Description = PolicyName;
            }

            if (policy != null)
            {
                // The PolicyId property provides the policyFeatureId parameter for the PolicyItemCollection indexer
                PolicyItem auditItem = policy.Items[PolicyLabel.PolicyId];
                if (auditItem == null)
                {
                    // Update policy with custom audit requirements
                    policy.Items.Add(PolicyLabel.PolicyId, LabelCustomData);
                }
                else
                {
                    // Modify existing policy with the custom audit requirements
                    auditItem.CustomData = LabelCustomData;
                }
            }

            ct.Update(true);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.