1

Need to find out the document set name from a field name called "Region". I need to set the doc set name to be whatever Region is (NW, SW, NE, SE, etc.) and move files from root of the document library to it's respected doc set. I dont mind hard coding the site or web url and doc lib url.

#Setup default variables
$webUrl = Get-SPWeb -Identity "http://CiscoIntranet/sites/VOIP"
$list = $webUrl.GetList("http://CiscoIntranet/sites/VOIP/ForwardTech")
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”) 

function ProcessMove {
   param($folderUrl)
   $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files)
            {   
                $docset=$($file.Title);
                $destinationFolderUrl = "http://http://CiscoIntranet/sites/VOIP/ForwardTech/" + $docset;
                $list.Items.MoveTo($destinationFolderUrl, true);
                $webUrl.Update();
            }
}
1
  • Are you asking about translating this into powershell and making it a function in a larger PowerShell script or do you want to make this a custom Cmdlet?
    – Dave Wise
    Commented Aug 26, 2011 at 16:40

2 Answers 2

1

The entirety of the SharePoint object model is available in Powershell so if you can do it in C# then you can almost certainly do it in Powershell. The question is simply the syntax to get there. I can get you started but you will have to flesh out the logic on your own based on what you are using as data sources.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[Microsoft.SharePoint.SPSite]$site = [Microsoft.SharePoint.SPSite]("http://SiteUrl/")

function MoveFile([Microsoft.SharePoint.SPWeb]$web, [string]$sourceUrl, [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]$docSet, [Microsoft.SharePoint.SPFile]$results)
{  
    [Microsoft.SharePoint.SPFile]$file = $web.GetFile($sourceUrl);
    [string]$destinationFolderUrl = $docSet.Folder.Url;
    if ($destinationFolderUrl.EndsWith("/") -eq $false) { $destinationFolderUrl += "/" }
    $file.MoveTo($destinationFolderUrl + $file.Name, $true); 
    $web.Update();    
    $results = $web.GetFile($web.Url + "/" + $destinationFolderUrl + $file.Name);
}

#get a handle to the parentweb
[Microsoft.SharePoint.SPWeb]$parentWeb = $site.OpenWeb("webUrl")

# put logic here to iterate through files, csv or array - whatever the source is... 

[Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]$destDocSet
#open the document set from the source

[Microsoft.SharePoint.SPFile] $movedFile

MoveFile $parentWeb, $url, $destDocSet, $moveFile

# do something with $moveFile...

#finish looping

#clean up the objects opened
$parentWeb.Dispose()
$site.Dispose()
1
  • Hi Dave, It seems I have to pass the url for the file. In my case, I like use foreach to go thru all the files in root of the doc library. Need to find out the document set name from a field name called "Region". I need to set the doc set name to be whatever Region is (NW, SW, NE, SE, etc.) and move files from root of the document library to it's respected doc set. I dont mind hard coding the site or web url and doc lib url. Commented Aug 26, 2011 at 19:57
1

This is the working code...

$siteURL="http://CiscoIntranet/sites/VOIP"
$docLib = "ForwardTech"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$collFiles=$web.GetFolder($docLib).Files
$count=$collFiles.Count
while($count -ne 0)
{
$item = $collFiles[$count-1].Item
$DocSet = $item["Region"]
Write-Host "$DocSet is the doc set. $collFiles[$count-1].Name is name"
$collFiles[$count-1].MoveTo($siteURL + "/" + $docLib + "/" + $DocSet + "/"  + $collFiles[$count-1].Name, $true)                       
$count--
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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