Question

I have a feature that creates a library in a site collection root, as we have 30000 site collections I cant activate it manually, I have to activate it with powershell.

The problem is that when I activate it manually in the browser it works fine, but in powershell I get this error.

I checked the Feature Activated code and I didnt see code related to SPCurrent so I dont see why its failing.

Error is:

Enable-SPFeature : Field not found: 'Lists.ClientBillingInstructionsUrl'.
At D:\lv\xxx.SP.InstallersGit\2.DMS\R4.8.2\Scripts\CustomFeatureUpgrade\ActivateFeatures.ps1:23 char:4
+    Enable-SPFeature -Identity $featureNameBillingInstructions -Url $spSiteCollec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], MissingFieldException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature

Feature activated code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            String listUrl = Constants.Lists.ClientBillingInstructionsUrl;
            String listName = Constants.Lists.ClientBillingInstructionsName;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(((SPWeb)properties.Feature.Parent).Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(((SPWeb)properties.Feature.Parent).ID))
                        {
                            try
                            {
                                LoggingService.LogInfo(LoggingCategory.Feature, String.Format("Entered feature with name '{0}' and id '{1}' for list creation. List to create: (name: '{2}' , url: '{3}').",
                                                                                              properties.Feature.Definition.DisplayName,
                                                                                              properties.Feature.Definition.Id,
                                                                                              listName,
                                                                                              listUrl));

                                SPList billingInstructionsLibrary = web.CreateList(
                                                                        listUrl, //name
                                                                        "", //description
                                                                        101,  //type
                                                                        true, //showinQuickLaunch
                                                                        true, //allowManagementOfContentTypes
                                                                        true, //enableVersioning
                                                                        true, //enableMinorVersions
                                                                        DraftVisibilityType.Reader, //draftVisibilityType
                                                                        false, //forceCheckout 
                                                                        false //enableModeration
                                                                        );

                                if (billingInstructionsLibrary != null)
                                {
                                    #region library specific settings
                                    billingInstructionsLibrary.Title = "-"; //this is a trick to force quicklaunch displaytext to change. (it ignores casing updates on exact same words)
                                    billingInstructionsLibrary.Update();
                                    billingInstructionsLibrary.Title = listName;
                                    billingInstructionsLibrary.MajorVersionLimit = 5;
                                    billingInstructionsLibrary.MajorWithMinorVersionsLimit = 5;
                                    billingInstructionsLibrary.Update();
                                    #endregion

                                    #region add content types
                                    billingInstructionsLibrary.AddListContentType(new SPContentTypeId(Constants.ContentTypes.Client.PONumber.ID));
                                    billingInstructionsLibrary.AddListContentType(new SPContentTypeId(Constants.ContentTypes.Client.BillingMethod.ID));
                                    #endregion

                                    //save changes (because otherwise delete of default CT wont succeed
                                    billingInstructionsLibrary.Update();

                                    #region remove default content type
                                    //delete content type 'document'
                                    billingInstructionsLibrary.DeleteListContentType("Document");
                                    #endregion

                                    #region views

                                    //Modify View "All items"
                                    SPView allDocumentsView = null;
                                    foreach (SPView view in billingInstructionsLibrary.Views)
                                    {
                                        if (view.Title.ToLower() == "all documents")
                                        {
                                            allDocumentsView = view;
                                            break;
                                        }
                                    }

                                    if (allDocumentsView != null)
                                    {
                                        allDocumentsView.ViewFields.DeleteAll();
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.DocIcon_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.LinkFilename_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.Title_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.Created_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.CreatedBy_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.Modified_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.ModifiedBy_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.Version_Name);
                                        allDocumentsView.ViewFields.Add(Constants.DefaultFields.FileSize_name);

                                        //Set view settings
                                        allDocumentsView.RowLimit = 30;
                                        allDocumentsView.IncludeRootFolder = false;
                                        allDocumentsView.Paged = true;
                                        allDocumentsView.Query = String.Format("<OrderBy><FieldRef Name=\"{0}\" Ascending=\"{1}\" /></OrderBy>",
                                                                               Constants.DefaultFields.LinkFilename_Name,
                                                                               "TRUE");

                                        allDocumentsView.Update();
                                    }
                                    #endregion

                                    billingInstructionsLibrary.Update();
                                    LoggingService.LogInfo(LoggingCategory.Feature, String.Format("List with name '{0}' and url '{1}' created.", listName, listUrl));
                                }
                                else
                                {
                                    throw new Exception(String.Format("List with name '{0}' and url '{1}' could not be found.", listName, listUrl));
                                }
                            }
                            catch (Exception exception)
                            {
                                LoggingService.LogError(LoggingCategory.Feature, exception);
                            }
                        }
                    }
                });
            }
            catch (Exception exception)
            {
                LoggingService.LogError(LoggingCategory.Feature, exception);
            }
        }

and this is the powershell code I use to activate the feature

#Library creation
$featureNameBillingInstructionsEnabled = Get-SPFeature  -Site $spSiteCollection -Identity $featureNameBillingInstructions -ErrorAction SilentlyContinue;
if($featureNameBillingInstructionsEnabled-eq $null)
{
   Enable-SPFeature -Identity $featureNameBillingInstructions -Url $spSiteCollection.Url
}
else
{
   Write-Host "Feature $featureNameBillingInstructionsEnabled already enabled";
}
Was it helpful?

Solution

It's a MissingFieldException so there is a problem with calling Constants.Lists.ClientBillingInstructionsUrl field.

Probably wrong .dll file is loaded into powershell process. This might happen if you deployed your new solution (with changed DLL) but did not restarted your powershell console / powershell ise.

Check if restarting powershell helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top