Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
share|improve this question
    
Not much I can do about improving my question or asking style if a down-vote goes unexaplained. –  DeeMac Nov 30 '12 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) –  SPArchaeologist Nov 30 '12 at 9:23
    
Also, more on the question content side, I would add the code for the addCT1/addct2 methods –  SPArchaeologist Nov 30 '12 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. –  DeeMac Nov 30 '12 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). –  SPArchaeologist Nov 30 '12 at 9:42
show 1 more comment

2 Answers 2

up vote 1 down vote accepted

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 ?

share|improve this answer
    
Would the stacktrace not include AddCT1 or AddCT2 though? Thanks a lot for your reply. +1. –  DeeMac Nov 30 '12 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. –  Sascha Nov 30 '12 at 9:32
add comment

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 :)

share|improve this answer
    
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. –  DeeMac Nov 30 '12 at 10:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.