22

Trying to modify ProjectPolicy Properties server side is giving me an error :

List<ProjectPolicy> availPolicies = ProjectPolicy.GetProjectPolicies(web);
ProjectPolicy policy = availPolicies.FirstOrDefault();
policy = availPolicies.Where(p => p.Name == "XYZPolicyName").FirstOrDefault();
policy.EmailSubject = "SiteName Deletion Notice";
policy.SavePolicy();

Getting an error at SavePolicy(): "The collection cannot be modified." Same is working for client side one. What could be the problem in server side?

Below link for reference, it's working at client side. Reference

Is there any other way of achieving it? I want to change policy email while creating new site collection. I have creating site collection thing ready. Only thing is I need to update code with policy email content. Above code gives error as mentioned. Is there any other way I can achieve it.

[Edited and added error logs]

Microsoft.SharePoint.SPException
Message=The collection cannot be modified.
StackTrace:
   at Microsoft.SharePoint.SPContentType.Update(Boolean updateChildren, Boolean ignoreSealedOrReadOnly, Boolean throwOnSealedOrReadOnly, IList`1 exceptions)
   at Microsoft.Office.RecordsManagement.InformationPolicy.Policy.Update()
   at Microsoft.Office.RecordsManagement.InformationPolicy.ProjectPolicy.Update()
   at ExpirationPolicyEmail.Program.Main(String[] args) in f:\SharePoint Apps\ExpirationPolicyEmail\ExpirationPolicyEmail\Program.cs:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
3
  • Good question! But it can be better if you provide the error log here too. As of now we can only guess what's wrong.
    – Benny Skogberg
    Commented Dec 15, 2015 at 8:29
  • 2
    added error logs. Commented Dec 15, 2015 at 10:54
  • 2
    Just to confirm you are not alone, I tried and got the same error on my environment. I could not find a solution. It may be a bug. But I guess you can probably stay with the csom solution. Commented Dec 17, 2015 at 2:24

3 Answers 3

0

Usually this exception occurs when you update properties of an object received as a result of LINQ calls.

You may try to finalize all the LINQ Where() calls with .ToArray() call in order to get an "independent" copy of object (list of objects) and then save this "independent" copy. Something like this:

List<ProjectPolicy> availPolicies = ProjectPolicy.GetProjectPolicies(web);
ProjectPolicy policy = availPolicies.Where(p => p.Name == "XYZPolicyName").ToArray().FirstOrDefault();
policy.EmailSubject = "SiteName Deletion Notice";
policy.SavePolicy();
1
  • This doesn't work. Commented Oct 9, 2019 at 6:09
0

Mask your SPSite context request with

SPSecurity.RunWithElevatedPrivileges(delegate()
                { 
// Your code within
});

This will override the permissions to the list.

1
  • This doesn't work. Commented Oct 9, 2019 at 6:08
0

With a little help from Reflection it can be done:

List<ProjectPolicy> availPolicies = ProjectPolicy.GetProjectPolicies(web);
ProjectPolicy policy = availPolicies.FirstOrDefault();
policy = availPolicies.Where(p => p.Name == "XYZPolicyName").FirstOrDefault();
policy.EmailSubject = "SiteName Deletion Notice";
object ct = policy.GetType().GetField(
    "contentType",
    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).
    GetValue(policy);
ct.GetType().GetField(
    "m_Pushingdown", 
    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).
    SetValue(ct, true);
policy.SavePolicy();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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