Friday, May 3, 2013

Powershell - Display Event Receivers for a specific Sharepoint List


$web=Get-SPWeb "http://siteURL"
$list=$web.Lists["ListName"]
$list.EventReceivers
$web.Dispose()

Display Names of the Event receivers 

$web=Get-SPWeb "http://SIteURL"
$list=$web.Lists["ListName"]

$list.EventReceivers |ForEach-Object {Write-host $_.Type  $_.Name}
$web.Dispose()

Powershell - Create Posts list to Blog site in Sharepoint


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$site=new-Object Microsoft.SharePoint.SPSite("http://siteurl/sites/test/")

$web=$site.OpenWeb()

$SPTemplate = $web.ListTemplates["Posts"]


$web.Lists.Add("Posts","Description",$SPTemplate)

Powershell - Delete list from Sharepoint site



[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$site=new-Object Microsoft.SharePoint.SPSite("http://siteurl/sites/test/")

$web=$site.OpenWeb()

$list=$web.Lists["ListName"]

$list.Delete()

Friday, April 26, 2013

Get SPUser from sharepoint SPGroup


List<SPUser> usersLst = web.Groups[“GroupName”].Users.OfType<SPUser>().ToList<SPUser>();

How to fetch user from multi-valued column in ItemEventReceiver



if you have muli-valued column of type Person or Group, the afterpropeties will give results in the format
"9;#Admin;#11;#Reader;#13;#Contribute";
To get the username or group names from the above string format, use the below code.

  char[] separators = new char[] { ';', '#' };
  object objgroupNamesNew = properties.AfterProperties["UserGroup"];
  string gNew = Convert.ToString(objgroupNamesNew);
  string[] gpnameNew = gNew.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  var groupNamesNew = gpnameNew.Where((gp, index) => index % 2 != 0);
  foreach (var grp in groupNamesNew)
            {
                Console.WriteLine(grp);
            }