Powershell 启用SPFeature:未找到字段

Powershell 启用SPFeature:未找到字段,powershell,sharepoint,Powershell,Sharepoint,我有一个在网站集根目录中创建库的功能,因为我们有30000个网站集,我无法手动激活它,我必须使用powershell激活它 问题是,当我在浏览器中手动激活它时,它可以正常工作,但在powershell中我会出现此错误 我检查了功能激活代码,但我没有看到与SPCurrent相关的代码,因此我不知道为什么它会失败 错误是: Enable-SPFeature : Field not found: 'Lists.ClientBillingInstructionsUrl'. At D:\lv\xxx.SP

我有一个在网站集根目录中创建库的功能,因为我们有30000个网站集,我无法手动激活它,我必须使用powershell激活它

问题是,当我在浏览器中手动激活它时,它可以正常工作,但在powershell中我会出现此错误

我检查了功能激活代码,但我没有看到与SPCurrent相关的代码,因此我不知道为什么它会失败

错误是:

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
功能激活代码:

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);
            }
        }
这是一个错误,因此调用
常量.list.ClientBillingInstructionsUrl
字段时出现问题

可能是错误的.dll文件加载到powershell进程中。如果您部署了新解决方案(使用更改的DLL),但未重新启动powershell控制台/powershell ise,则可能会发生这种情况


检查重新启动powershell是否有帮助。

哇,谢谢你,伙计,这么多年来一直从事SP开发,从未遇到过这样的问题,2天时间检查代码、xml文件、包内容等
#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";
}