如何通过PowerShell脚本创建外部列表?
我有一个解决方案,我使用相当大的PowerShell脚本部署到SharePoint 2010服务器。当脚本即将使用BCS模型创建外部列表作为其数据源时,我得到异常<nativehr>0x80131600</nativehr><nativestack></nativestack>
以下是用于创建列表的powershell代码的摘录:
# Remove the BCS model.
# Deploy the WSP package, including the BCS model.
# Delete the external list if it already exists.
...
$currentEntityName = $Entity.Name
Write-Host "BCS: Entity '$currentEntityName' - Granting permissions"
Grant-SPBusinessDataCatalogMetadataObject -Identity $Entity -Right $EntityPermissions -Principal $principal -ErrorAction SilentlyContinue
Grant-SPBusinessDataCatalogMetadataObject -Identity $Entity -Right $EntityPermissions -Principal $principalAllFlisUsers -ErrorAction SilentlyContinue
Grant-SPBusinessDataCatalogMetadataObject -Identity $Entity -Right $EntityPermissions -Principal $principalAllFlisUsersDemo -ErrorAction SilentlyContinue
Grant-SPBusinessDataCatalogMetadataObject -Identity $Entity -Right $EntityPermissions -Principal $principalAppPoolUser -ErrorAction SilentlyContinue
...
$ds = New-Object -TypeName Microsoft.SharePoint.SPListDataSource
$ds.SetProperty("LobSystemInstance", $instanceName)
$ds.SetProperty("EntityNamespace", $ns)
$ds.SetProperty("Entity", $name)
$ds.SetProperty("SpecificFinder", $sf.Name)
...
$site = Get-SPSite -Identity $SiteUrl
$web = $site.OpenWeb($webNameString)
$newList = $web.Lists.TryGetList("$ExternalListName")
if (!$newList)
{
$ListGUID = $web.Lists.Add("$ExternalListName", "", [String]::Format($ListUrl, $ExternalListUrl), $ds) | out-null
$web.Lists[$ListGUID].Update()
$web.Update()
}
当我在接近结束时调用$web.Lists.Add()
方法时抛出异常。
在ULS-log中,我发现了一个可能相关的异常:
The type ' ProjectName.Common.CustomFieldTypes.ListSpecificCustomField,ProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ff8505e11ea28a7e ' could not be deserialized. Exception: System.TypeLoadException: Could not load type 'ProjectName.Common.CustomFieldTypes.ListSpecificCustomField' from assembly 'ProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ff8505e11ea28a7e '. at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName) ...
(注意:我已将实际项目名称替换为ProjcetName并替换了自定义字段的名称)
该列表在/_layouts/sitemanager.aspx
中可见,但未显示任何数据。当powershell脚本测试它是否已存在时,测试返回true。
奇怪的是,当我重新运行整个PowerShell脚本时,列表是正确创建的。然而,仅仅循环引用的脚本行以获得正确的部署是不够的。这使我认为BCS模型没有完全部署,或者BCS模型以某种方式缓存,并且在部署脚本运行时抵制变化。