20

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()
  • 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 Dec 15 '15 at 8:29
  • 2
    added error logs. – Bhaskar Dhone Dec 15 '15 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. – Sylvain Gantois Dec 17 '15 at 2:24
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();
0

Mask your SPSite context request with

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

This will override the permissions to the list.

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.