1

I'm writing a powershell script that creates folders in a Records Library using SharePoint document routing rules, and am able to set the conditions, name, description etc. Where I'm having difficulty is explicitly setting the Target Location field so that the folder is automatically created for the unique value of a property of the document. Essentially, I want to be able to set what I have below as part of my powershell script. Anybody have any tips that could lead me in the right direction?

Desired Behaviour

Here is the code I have tried so far:

#store the config & global variables
$ctConfig = [xml](Get-Content $configXMLPath);
$RecordCenterURL = "http://spvm/sites/assessrc"
$web = Get-SPWeb $RecordCenterURL
$docLib = $web.Lists["Record Library"]
$conField = $docLib.Fields.GetField("Confidential Document")
$ppmField = $docLib.Fields.GetField("PPM ID")
foreach($xmlContentType in $ctConfig.ContentTypes.ContentType)
{
    #Write-Host "Creating rules for" $xmlProdFam.Name "..."

    #Create Rule
    [Microsoft.SharePoint.SPSite]$site = Get-SPSite $RecordCenterURL
    [Microsoft.SharePoint.SPWeb]$web = Get-SPWeb $RecordCenterURL
    [Microsoft.SharePoint.SPContentType]$ct = $site.RootWeb.ContentTypes[$xmlContentType]
    [Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule]$rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($web)

    $rule.ConditionsString = "<Conditions><Condition Column='" + $conField.Id + "|ConfDoc|Confidential Document' Operator='IsEqual' Value='False'></Condition></Conditions>" 
    $rule.CustomRouter = ""
    $rule.Name = "Non-Confidential " + $ct.Name + "Routing Rule"
    $rule.Description = "Routes '" + $ct.Name + "' documents from the '" + $xmlProdFam.Name + "' Product Family to the records library"
    $rule.ContentTypeString = $xmlContentType.Name
    $rule.RouteToExternalLocation = $false
    $rule.Priority = "5"
    $rule.TargetPath = $site.SubFolders["PPM ID"];
    $rule.enabled = $true
    $rule.Update()

}
  • Can you post the code you have tried so far – Amal Hashim Aug 8 '16 at 17:44
1

Was able to use AutoFolderSettings within PowerShell to create the rule. Here is the code that I used to get this to work in case anybody else stumbles upon this.

$configXMLPath = "C:\temp\ContentTypeListing.xml"
#store the config & global variables
$ctConfig = [xml](Get-Content $configXMLPath);
$RecordCenterURL = "{URL to Record Center}"
$web = Get-SPWeb $RecordCenterURL
$docLib = $web.Lists["Record Library"]
$conField = $docLib.Fields.GetField("Confidential Document")
$ppmField = $docLib.Fields.GetField("PPM ID")
#loop through each Content Type (Non-Confidential Rules)
foreach($xmlContentType in $ctConfig.ContentTypes.ContentType)
{
    #Write-Host "Creating rules for" $xmlProdFam.Name "..."
    #Create Rule
    [Microsoft.SharePoint.SPSite]$site = Get-SPSite $RecordCenterURL
    [Microsoft.SharePoint.SPWeb]$web = Get-SPWeb $RecordCenterURL
    [Microsoft.SharePoint.SPContentType]$ct = $site.RootWeb.ContentTypes[$xmlContentType]
    [Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule]$rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($web)

    $rule.ConditionsString = "<Conditions><Condition Column='" + $conField.Id + "|ConfDoc|Confidential Document' Operator='IsEqual' Value='False'></Condition></Conditions>" 
    $rule.CustomRouter = ""
    $rule.Name = "Non-Confidential " + $ct.Name + "Routing Rule"
    $rule.Description = "Routes '" + $ct.Name + "' documents from the '" + $xmlProdFam.Name + "' Product Family to the records library"
    $rule.ContentTypeString = $xmlContentType.Name
    $rule.RouteToExternalLocation = $false
    $rule.Priority = "5"
    $rule.TargetPath = $web.Lists["Record Library"].RootFolder.ServerRelativeUrl
    $rule.AutoFolderSettings.Enabled = $true
    $rule.AutoFolderSettings.AutoFolderPropertyName = $ppmField.InternalName
    $rule.AutoFolderSettings.AutoFolderPropertyId = $ppmField.Id
    $rule.AutoFolderSettings.AutoFolderFolderNameFormat = "%2"
    $rule.enabled = $true
    $rule.Update()
}
0

I believe you are using EcmDocumentRouterRule class for defining the rule.

If that is the case then you can use the property TargetPath to define the Target Location.

[Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule]$rule = New-Object Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRouterRule($web)

$rule.TargetPath = "Target Location URL"
$rule.Enabled = $true
$rule.Update() 

For generating Auto Folder Name you can use below script

// Replace with a format of your choice. Place holder %1 will be replaced by the name of the field and %2 will be replaced by the value of the field for the document.
const string autoFolderNameFormat = "Contract %1 - %2";
// Get a handle to the rule auto folder settings.
DocumentRouterAutoFolderSettings autoFolderSettings = organizeDocument.AutoFolderSettings;
// Configure AutoFolderSettings for this rule based on the "Business Division" Taxonomy field.                 
autoFolderSettings.AutoFolderPropertyName = autoFolderField.Title;
autoFolderSettings.AutoFolderPropertyInternalName = autoFolderField.InternalName;
autoFolderSettings.AutoFolderPropertyId = autoFolderField.Id;
// Term store Id required to get the value of the field from the document. Required for TaxonomyField types.
autoFolderSettings.TaxTermStoreId = autoFolderField.SspId;
// Set a format for the name of the folder. 
autoFolderSettings.AutoFolderFolderNameFormat = autoFolderNameFormat;
// Enabled automatic folder creation for values of the "Business Division" field.
autoFolderSettings.Enabled = true;
  • I'm doing that in other scripts, but I need to be able to add the 'PPM ID' unique value for each rule. This may change per document coming in, so I can't include a single 'Target Location URL' – Kelvin Hoyle Aug 8 '16 at 17:49
  • Read my updated answer.. For the full example open up the URL.. – Amal Hashim Aug 8 '16 at 17:54
  • Actually, on second thought, I don't think this is going to work for me as the unique value needs to be used on creation on the folder when the document is processed and not when the rule is created. – Kelvin Hoyle Aug 8 '16 at 18:25
  • The foldername is created based on the rule expression.. What the code does is the same as what you are planning to do on the UI... – Amal Hashim Aug 8 '16 at 18:27
  • Sorry.. not quite getting it. The Auto Folder Name section seems to be C#, not PowerShell. I was hoping to keep this strictly to PowerShell. – Kelvin Hoyle Aug 8 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.