0

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
6
  • Not much I can do about improving my question or asking style if a down-vote goes unexaplained. Commented Nov 30, 2012 at 9:02
  • Don't know what was the reason (don't know who was the downvoter), but if you want to improve the question I would edit the title to be more specific and fix the formatting error in the first code sample (there is an unclosed string, so the coloring get wrong) Commented Nov 30, 2012 at 9:23
  • Also, more on the question content side, I would add the code for the addCT1/addct2 methods Commented Nov 30, 2012 at 9:24
  • Thanks a lot @SPArchaeologist - will update now. Appreciate your feedback. I'm struggling to be any more specific to be honest, I've provided all of the context I can, I'll try and improve that. Commented Nov 30, 2012 at 9:27
  • I have edited the title - should be more specific now (we want people who read the question list to have an idea of the problem before opening the question - that way ones that may know the answer won't skip over "easy rep points" ^_^). That said, IMHO the question now look good (maybe we can add some more tags). Commented Nov 30, 2012 at 9:42

2 Answers 2

1

A duplicate field name "5749c691-eef1-4d54-9e98-9039babe331f

I think there is a redundant fieldname assigned before an update occurs. Maybe in the AddCT1 or AddCT2 method ?

2
  • Would the stacktrace not include AddCT1 or AddCT2 though? Thanks a lot for your reply. +1. Commented Nov 30, 2012 at 9:02
  • When you didn't compile it into debug mode. It won't be visible i mean. But I'm not really sure about this. Commented Nov 30, 2012 at 9:32
1

have you tried to set allowunsafeupdates?

 private void AddcontentTypesToDocumentLibrary(SPWeb web, SPList list) { SPSite site = web.Site; SPWeb newWeb = web; SPSecurity.RunWithElevatedPrivileges(delegate() { web.AllowUnsafeUpdates = true; 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(); } } web.AllowUnsafeUpdates = false; }); } 

more info can be found here:

http://blog.animesh.co.in/2010/03/sharepoint-spsiteallowunsafeupdates.html

this also could be some help to you if above doesnt work:

Removing Duplicated Field Links from Content Types

http://www.scolts.com/?p=96

hope it helps :)

1
  • Thanks a LOT for your answer. I'll give this a go, but I'm not sure it's permissions based. Testing seems to prove it's only environment specific. Thanks again. Commented Nov 30, 2012 at 10:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.