• Home
  • About
Subscribe: Posts | Comments | E-mail
  • SharePointTopics on Microsoft SharePoint Technology
  • Silverlight, WPFSilverlight, Windows Presentation Foundation
  • MiscellaneousRandom bits and rants

Rouslan Grabar on NET, SharePoint, SilverLight, WPF and other technology related topics

Posts Tagged ‘Powershell’


Posted on October 9, 2009 - by Rouslan Grabar

Move list item into a subfolder in the same list

Someone asked how to copy the list item into a subfolder on the same list. The code the guy provided that used SPListItem.CopyTo(url) did not work for him and was yelling the following:

Invalid URI: The format of the URI could not be determined.

The reason was the fact the guy used destination item property Url. This property is a relative Url, but the CopyTo method requires absolute URL.

Another caveat is that even if used properly, the method was throwing the following exception message:

Source item cannot be found.  Verify that the item exist and that you have permission to read it.

Considering the fact that we are moving item the way to overcome this is to use this approach – please see the Powershell snippet below:

#this is p o w e r s h e l l code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#returns the SPSite at the specified URL
function getSPSite ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   return New-Object Microsoft.SharePoint.SPSite("$webUrl");
}
$site = getSPSite($url) as [Microsoft.SharePoint.SPSite];
[Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb("/website");
[Microsoft.SharePoint.SPList]$list = $web.Lists["listname"];
[Microsoft.SharePoint.SPListItem]$srcItem  = $list.GetItemById(1);
[Microsoft.SharePoint.SPListItem]$destItem = $list.GetItemById(2);
[String]$srcUrl = $web.Url + "/" +$srcItem.Url;
#here we construct the destination URL for the item
[String]$destUrl = $web.Url + "/" +$destItem.Url + "/" + $srcItem.ID + "_.000";
[Microsoft.SharePoint.SPFile]$srcFile = $web.GetFile($srcUrl);
$srcFile.MoveTo($destUrl);


  • Recent Posts

    • SharePoint 2010: Проектирование списков и схем [techdays.ru]
    • Доклад по PerformancePoint Server 2007 на сайте TechDays.ru
    • How to allow only Folder items to be created in the List root folder
    • Move list item into a subfolder in the same list
    • Краткий обзор возможностей SharePoint 2010 для конечных пользователей
  • Tags

    Для новичков Customization Developer Utility Fileds For Beginners IIS mac os x Misc MOSS MSDN photosynth Powershell Russian Screencast SharePoint SharePoint 2010 SharePoint Designer Silverlight, WPF Snippet SPItemEventReceiver techdays timer job Toolbox tricks troubleshooting Virtualisation VSeWSS wcf web service Windows windows authentication WPF WSS
  • Blogroll

    • My personal journal in Russian.
    • Picture Downloader Software
    • Russian SharePoint Community
  • Archives

    • March 2010
    • November 2009
    • October 2009
    • July 2009
    • May 2009
    • April 2009
    • March 2009
© 2009 Rouslan Grabar. All Rights Reserved.