Judging by the stack trace, one of the following lines of code is giving me an error:
private void AddcontentTypesToDocumentLibrary(SPWeb web, SPList list)
{
SPSite site = web.Site;
SPWeb newWeb = web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedSite = new SPSite(site.ID))
{
using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(web.ID))
{
list = ElevatedWeb.Lists[list.Title];
list.ContentTypesEnabled = true;
SPContentType ct = ElevatedWeb.AvailableContentTypes["ContentType1"];
AddCT1(list, ct);
ct = ElevatedWeb.AvailableContentTypes["ContentType2"];
AddCT2(list, ct);
list.Update();
}
}
});
}
Code for CT1 (identical to CT2):
private void CT1(SPList list, SPContentType ct)
{
list.ContentTypes.Add(ct);
list.Update();
}
The error and stack trace is:
A duplicate field name "5749c691-eef1-4d54-9e98-9039babe331f" was found.
[SPException: A duplicate field name "5749c691-eef1-4d54-9e98-9039babe331f" was found.]
Microsoft.SharePoint.SPFieldLinkCollection.Add(SPFieldLink fieldLink) +1445
Microsoft.SharePoint.SPContentType.UpdateFieldsCollection(Dictionary`2 cachedFields, Guid addedField) +521
Microsoft.SharePoint.SPContentType.ProvisionFieldOnList(SPField field, Boolean bRecurAllowed) +2355
Microsoft.SharePoint.SPContentType.ProvisionFieldsOnList() +634
Microsoft.SharePoint.SPContentType.DeriveContentType(SPContentTypeCollection cts, SPContentType& ctNew) +671
Microsoft.SharePoint.SPContentTypeCollection.AddContentTypeToList(SPContentType contentType) +2319
Microsoft.SharePoint.SPContentTypeCollection.AddContentType(SPContentType contentType, Boolean updateResourceFileProperty, Boolean checkName, Boolean setNextChildByte) +242
Microsoft.SharePoint.SPContentTypeCollection.Add(SPContentType contentType) +24
[PROJECT NAME].[CLASS NAME].Layouts.[PROJECT NAME].[CLASS NAME].<>c__DisplayClassa.<AddcontentTypesToDocumentLibrary>b__9() +316
Microsoft.SharePoint.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2() +729
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27491190
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +27194329
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +93
[PROJECT NAME].[CLASS NAME].Layouts.[PROJECT NAME].[CLASS NAME].[CLASS NAME].AddcontentTypesToDocumentLibrary(SPWeb web, SPList list) +156
[PROJECT NAME].[CLASS NAME].Layouts.[PROJECT NAME].[CLASS NAME].[CLASS NAME].CreateSubSite(SPSite site) +582
[PROJECT NAME].[CLASS NAME].Layouts.[PROJECT NAME].[CLASS NAME].[CLASS NAME].btnOk_Click(Object sender, EventArgs e) +120
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981
Given the fact the stack trace doesn't indicate that an other method (AddCT1 or AddCT2) was called, then I'm assuming it's with the call to Update() (but would this not have been shown in the stack trace?) or one of the lines were I set ContentTypesEnabled or I get the CT.
I'm really struggling here.
Please note, this is environment specific. On development I do not get this error.
I have separated the content types out into separate methods (CT1/CT2) purely to push the stack trace into giving me a better idea of where it's occurring.
Can I also, rather ignorantly, request that we don't spend time discussing the efficiency or elegance of my code - I just need to get this working asap for now, I appreciate there's a lot of room for refactoring.
What I've Tried (or what I'm about to try)
- Separating each line of code into separate methods in order to give me a 'better' stack trace
- I've also tried adding the content types one by one through the UI