This is how you can rename a SPFolder. There is no need to use MoveTo() as suggested by others.
The user has to have rights to do it, or enclose the whole thing inside a
var siteId = "Guid of my site";
var webUrl= "Server relative path to my web";
SPSecurity.RunWithElevatedPrivileges(delegate(){
using (SPSite elevatedSite = new SPSite(siteId))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webUrl))
{
string requestFolderUrl = webUrl+ "/MYLIST/OldFolderName";
SPFolder requestFolder = elevatedWeb.GetFolder(requestFolderUrl);
if (requestFolder.Exists)
{
requestFolderItem[SPBuiltInFieldId.Title] = "NewName"; //not really needed for renaming
//requestFolderItem[SPBuiltInFieldId.BaseName] = "NewName"; //this Fails.don't know why
requestFolderItem["BaseName"] = "NewName"; //this Works!
//don't want to update the Modified time nor the modified by Info
//If you want to modify this info use Update, and avoid being in a RunWithElevatedPrivileges
///because else you would have the System User registered
requestFolderItem.SystemUpdate(false);
}
}
}
}
Tuesday, February 28, 2012
Thursday, February 16, 2012
Remove Event Handlers from SPList Powershell
This is how you can delete Event handlers from a SPList
$webUrl= "http://yoursite.com/subsite"
$listName = "myList"
$site = Get-SPSite($siteUrl)
$web = $site.OpenWeb()
$listWithEventHandlers = $web.Lists[$listName]
#you can find out the index of the eventhadler through a selector, or just beeing lazy
#and loop the eventhanlders
$allEvenReceivers = $list.EventReceivers
foreach($ev in $allEvenReceivers )
{
write-host $ev.Name;
}
$index = 0
$eventHandlerToDelete = $listWithEventHandlers.EventReceivers[$index]$eventHandlerToDelete.Delete()
$listWithEventHandlers.Update()
$web.Dispose()
$site.Dispose()
$webUrl= "http://yoursite.com/subsite"
$listName = "myList"
$site = Get-SPSite($siteUrl)
$web = $site.OpenWeb()
$listWithEventHandlers = $web.Lists[$listName]
#you can find out the index of the eventhadler through a selector, or just beeing lazy
#and loop the eventhanlders
$allEvenReceivers = $list.EventReceivers
foreach($ev in $allEvenReceivers )
{
write-host $ev.Name;
}
$index = 0
$eventHandlerToDelete = $listWithEventHandlers.EventReceivers[$index]$eventHandlerToDelete.Delete()
$listWithEventHandlers.Update()
$web.Dispose()
$site.Dispose()
Access MasterPage catalog with Powershell
To acces the Masterpage catalog via Powershell, and then use it as a list the following script might help
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear
$siteUrl = "http://yourserver.com"
$masterPageCatalog = "_catalogs/masterPage"
$site = Get-SPSite $siteUrl
$rootWeb = $site.RootWeb;
$folder = $rootWeb.GetFolder($masterPageCatalog)
$masterPageCatList = $rootWeb.Lists[$folder.ParentListId]
#do some list operations on it
$rootWeb.Dispose()
$site.Dispose()
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear
$siteUrl = "http://yourserver.com"
$masterPageCatalog = "_catalogs/masterPage"
$site = Get-SPSite $siteUrl
$rootWeb = $site.RootWeb;
$folder = $rootWeb.GetFolder($masterPageCatalog)
$masterPageCatList = $rootWeb.Lists[$folder.ParentListId]
#do some list operations on it
$rootWeb.Dispose()
$site.Dispose()
Subscribe to:
Posts (Atom)