I'm trying to create retention policies for some of the content types in a document library. The first one is created fine but when I go to create one for the second content type I get the above exception.

The calls to this function are like this:

SPList documentLibrary = web.Lists.TryGetList(Constants.MyDocumentsLibraryName);

if (documentLibrary == null)
        "Document Library not found in site " + web.Title);

web.AllowUnsafeUpdates = true;

AddRetentionPolicy(Constants.TestContentType, web, documentLibrary, site.Url);
AddRetentionPolicy(Constants.AnotherContentType, web, documentLibrary, site.Url);



And the function itself is:

public static void AddRetentionPolicy(String contentTypeName, SPWeb web, 
    SPList documentLibrary, string siteUrl)
    Logger.LogVerbose(LogCategory.Setup, "Getting content type " + contentTypeName);
    SPContentType contentType =
    if (contentType == null)
        Logger.LogVerbose(LogCategory.Setup, "Cannot find content type " + 
             contentTypeName + ", skipping.");
        Policy policy = Policy.GetPolicy(contentType);
        if (policy != null)
            Logger.LogVerbose(LogCategory.Setup, "Policy already exists for "
                + contentTypeName + ", deleting.");

        Policy.CreatePolicy(contentType, null);

        policy = Policy.GetPolicy(contentType);
            GeneratePolicyItemXML(Constants.retentionPeriod, Constants.retentionPeriodUnits, 
            web.Title, siteUrl, web.ServerRelativeUrl));


The error is occurring on the line:

Policy.CreatePolicy(contentType, null);
  • any idea? I'm getting the same issue :( – Verthosa Aug 22 '16 at 8:07

Solution: don't pass the list in your method, re-get it each time in the function. Reason: because of the policy that was updated for the content type, your list is also updated so solution is to re-get the list

SPList documentLibrary = web.Lists.TryGetList(Constants.MyDocumentsLibraryName);

before getting the SPContentType

| improve this answer | |

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.