Enable-SPFeature:找不到字段

时间:2018-04-26 14:54:32

标签: powershell sharepoint

我有一个在网站集根目录中创建库的功能,因为我们有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);
            }
        }

这是我用来激活功能的powershell代码

#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";
}

1 个答案:

答案 0 :(得分:1)

这是MissingFieldException,因此调用Constants.Lists.ClientBillingInstructionsUrl字段时出现问题。

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

检查重启powershell是否有帮助。