当前位置: 代码迷 >> SharePoint >> items 归档脚本有关问题
  详细解决方案

items 归档脚本有关问题

热度:287   发布时间:2016-05-02 07:05:18.0
items 归档脚本问题
我想对一个library里的items移动到该库下的子目录中, 在网上有个例子,就是做这样的动作, 它是把items 移动到该库下
/Archive/当天日期  如 /Archive/20140521, 该例子除了 几个  where 前要加 “|”要修改外, 其他不变就可以运行; 但运行老遇到不存在归档目录的错误, 但这目录是该powershell生成, 现都不清楚是哪里问题, 请各位帮忙看看,谢谢!

http://sharepointstruggle.blogspot.hk/2010/07/sharepoint-vs-powershell-moving-list.html



$WebURL = "http://pwtestsrv01/Applications";
$ListDisplayName = "TEST";
$ArchiveFolderName = "Archive";

function LoadWSSAssembly{
write-host "Loading WSS Assembly..."  
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 
write-host "Done..."  
}

function folderExists([Microsoft.SharePoint.SPFolder] $Folder, [string]$FolderName){
    return !(($Folder.SubFolders | where {$_.Name -eq $FolderName}) -eq $null);
}

function getFolder([Microsoft.SharePoint.SPFolder] $Folder, [System.String] $folderName)
 {    write-host "Searching for folder '" $folderName "' in SPFolder located at " $Folder.Url "...";    
 if (!(folderExists $Folder $folderName)) {
    write-host "Folder Not Found - Creating a new one..."; 
    $folderItem = $list.Items.Add($Folder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folderName);
    $folderItem.SystemUpdate();
    $list.Update();            #Need to get a fresh reference of the folder to ensure we can see the new one just created:            
    $Folder = $Folder.ParentWeb.GetFolder($Folder); }    
    return $Folder.SubFolders  | where {$_.Name -eq $FolderName};
 }

function getArchiveFolder([Microsoft.SharePoint.SPList] $List){
#Get Reference to the Archive Root Folder:    
[Microsoft.SharePoint.SPFolder] $archiveRoot = getFolder $List.RootFolder $ArchiveFolderName;
        #Get Reference to the daily archive folder:    
        $nowTimeInString = [System.DateTime]::Now.ToString("yyyyMMdd");    
        [Microsoft.SharePoint.SPFolder] $todaysArchiveFolder =  getFolder $archiveRoot $nowTimeInString;    
        return $todaysArchiveFolder;}

function moveItems
{ trap
    {#make sure we dispose of these in the event of an error to avoid memory leaks:
     write-host "Error - disposing of objects...";
     $Web.Dispose();
     $Site.Dispose();
     }
     [Microsoft.SharePoint.SPSite] $Site = New-Object Microsoft.SharePoint.SPSite($WebURL);
     [Microsoft.SharePoint.SPWeb] $Web = $Site.OpenWeb();
     [Microsoft.SharePoint.SPList] $List = $Web.Lists[$ListDisplayName];
     [Microsoft.SharePoint.SPFolder] $FolderToMoveTo = getArchiveFolder $List;     
     $ItemMoveCount=0;
     $Query = New-Object Microsoft.SharePoint.SPQuery;
     $Query.Folder = $list.RootFolder;
     $List.GetItems($Query)  | Where {$_.ContentType.Name -ne "Folder"}     | foreach-object {
     #Line below will simply output to console and demonstrates another .NET call        
     [System.String]::format("Moving Item {0} with ID {1}",$_.Title, $_.ID.ToString());
     #[System.String]::format("Moving Item {0}",$_.Title);
     #[System.String]::format("Moving Item {0}",$_.Url);
     [System.String]::format("{0}",$FolderToMoveTo.Url);
     [System.String]::format("{0}/{1}",$WebURL,$FolderToMoveTo.Url);
     $Web.GetFile($_.Url).MoveTo([System.String]::format("{0}/{1}_.000",$FolderToMoveTo.Url,$_.ID.ToString()));
     #$Web.GetFile($_.Url).MoveTo([System.String]::format("{0}/{1}",$FolderToMoveTo.Url,$_.Name));
     #$Web.GetFile($_.Url).MoveTo([System.String]::format("{0}/{1}",$FolderToMoveTo.Url,$_.ID.ToString()));
  相关解决方案