Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
            }
}
share|improve this question
    
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 Aug 26 '11 at 16:40
add comment

2 Answers

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()
share|improve this answer
    
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. –  Nancy o'Donnell Aug 26 '11 at 19:57
add comment
up vote 1 down vote accepted

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--
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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