1

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)
{
    Logger.LogVerbose(LogCategory.Setup,
        "Document Library not found in site " + web.Title);
    return;
}

web.AllowUnsafeUpdates = true;

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

documentLibrary.Update();

}

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 =
        documentLibrary.ContentTypes[contentTypeName];
    if (contentType == null)
    {
        Logger.LogVerbose(LogCategory.Setup, "Cannot find content type " + 
             contentTypeName + ", skipping.");
    }
    else
    {
        Policy policy = Policy.GetPolicy(contentType);
        if (policy != null)
        {
            Logger.LogVerbose(LogCategory.Setup, "Policy already exists for "
                + contentTypeName + ", deleting.");
            Policy.DeletePolicy(contentType);
        }

        Policy.CreatePolicy(contentType, null);

        policy = Policy.GetPolicy(contentType);
        policy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration",
            GeneratePolicyItemXML(Constants.retentionPeriod, Constants.retentionPeriodUnits, 
            web.Title, siteUrl, web.ServerRelativeUrl));

        contentType.Update();
    }
}

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
0

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

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.