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

I have the following code and I havent been able to find what the problem is. The exception is raised SOMETIMES in the last line of enable keywords, in the Update.

The thing is we create a site manually from code: then from code we also add a feature, and the problem is happening somewhere in the feature activated or some of its methods.

/// <param name="list"></param>
        public static void EnableEnterpriseKeywords(this SPList list)
        {
            Logger.LogDebug("SPListExtensions", "EnableEnterpriseKeywords(this SPList list)", "BEGIN");
            try
            {
                Assembly taxonomyAssembly = Assembly.LoadWithPartialName("Microsoft.SharePoint.Taxonomy");
                Type listFieldSettings = taxonomyAssembly.GetType("Microsoft.SharePoint.Taxonomy.MetadataListFieldSettings");
                object listSettings = listFieldSettings.GetConstructor(new Type[] { typeof(SPList) }).Invoke(new object[] { list });
                listFieldSettings.GetProperty("EnableKeywordsField", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(listSettings, true, null);
                listFieldSettings.GetProperty("EnableMetadataPromotion", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(listSettings, true, null);
                listFieldSettings.GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(listSettings, null);
            }
            catch (Exception ex)
            {
                Logger.LogError("SPListExtensions", string.Format("EnableEnterpriseKeywords(this SPList list) - Error activating on list {0}", list.Title), ex);
                throw;
            }
            Logger.LogDebug("SPListExtensions", "EnableEnterpriseKeywords(this SPList list)", "END");
        }
share|improve this question
    
Are you sure this is executed in the context of someone with Permissions change rights?? E.g. Open the SPWeb with RunAsPriviledged? Also FALSE on BreakRoleInheritance means NO Copy of existing permissions from Parent - could generate issues with user running it is not Site Collection Administrator –  C. Marius - MVP Aug 7 '12 at 15:15
    
I just pasted the entire code, sorry for that, the error has to be in the order of AllowUnsafeUpdates somewhere I suppose, I have tried for 8 hours and I cant find the problem, at the beginning I thought it was with enable enterprise keywords, but sometimes the exception is raised in setsecurity, sometimes somewhere else. –  L.V. Sharepoint Architect Aug 7 '12 at 15:26
    
try to execute an Update on spWeb before moving on as it seems you already made some changes. Move the AllowUnsafeUpdates before making any change on the SPWeb. If you perform Content Type related changes it would be good to get a NEW SpWeb instance after Updating, so basically have to USING clauses in the same SPSite processing. –  C. Marius - MVP Aug 7 '12 at 15:31

1 Answer 1

up vote 2 down vote accepted

Q: Did you have AllowUnsafeUpdates= True on the SpWeb?

I must say the way you use Reflection is interesting. Did you know that enabling Enterprise Keywords column is simple a matter of checking whether field exist and then adding it to the list as in:

 SPList list = SPContext.Current.Web.Lists.TryGetList("TargetList");
if (list != null)
                    {
                        SPField field = list.ParentWeb.AvailableFields["Enterprise Keywords"];
                        if (!list.Fields.ContainsField("Enterprise Keywords"))
                        {
                            list.Fields.Add(field);
                            list.Update();
                        }
                   }
share|improve this answer
    
nice idea, its not my code, I am trying to debug a problem I am having, but after checking, I think the problem comes from before. pls see my updated post in 1 minute –  L.V. Sharepoint Architect Aug 7 '12 at 15:07
    
I just pasted the entire code, sorry for that, the error has to be in the order of AllowUnsafeUpdates somewhere I suppose, I have tried for 8 hours and I cant find the problem, at the beginning I thought it was with enable enterprise keywords, but sometimes the exception is raised in setsecurity, sometimes somewhere else. –  L.V. Sharepoint Architect Aug 7 '12 at 15:27

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.