Wednesday, April 8, 2015

How to Remove invalid users from SharePoint Web Applications


Here is a PowerShell script to remove invalid users from the SharePoint Web Applications

 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Get all the Site Collections inside the Web Application
$SiteColl = Get-SPWebApplication "http://abc.xyz.com" | Get-SPSite -Limit All
 
#Define the List of Users
$Users =@('domain\abc',’domain\xyz’)

#Iterate through all the Site Collections
foreach($site in $SiteColl)
{
 #Iterate through users to Remove
 foreach($user in $Users)
 {
   #Check if User Exists in Site
   if ($Site.RootWeb.SiteUsers | Where {$_.LoginName -eq $User})
   {
      $Site.RootWeb.SiteUsers.Remove($User)
   Write-Host "$($user) has been Removed from the $($Site.RootWeb.URL)"
   }
  }
}

Friday, April 3, 2015

How to Remove a User from a SharePoint 2013 Site Collection


As I was troubleshooting a recent issue with the User display name, where there was an change to a user account in Active Directory (like change in the last Name) . However, the change did not reflect in the site collection as expected.  Verified Profile sync and the last name is correct in the profile.

Here are the steps that I performed to fix the issue.

1.      As a site collection administrator, click Site Actions –> Site Permissions

2.      Click into any existing SharePoint group and the URL will be something like http://server/_layouts/people.aspx?MembershipGroupID=298.

3.      Change the 298 to a 0 and the list should now show you All People.

4.      Find the user whose last name is an issue, click the checkbox by their name.  Click Actions, Delete Users from Site Collection.

5.
      
Now add the user back to the appropriate group and you would notice the correct last name as expected.

Thursday, September 25, 2014

PowerShell Script to download SharePoint List attachments


Here is a PowerShell script for downloading SharePoint List Item attachments. When the code is run all attachments are downloaded. Subfolders are created based on the Assigned To.


$webUrl = "Your Site Name"
$listname = "Your List Name"
$tempLocation = "Folder Location"
$site = new-object Microsoft.SharePoint.SPSite($webUrl)
$web = $site.OpenWeb()
$list = $web.Lists[$listname]
foreach ($listItem in $list.Items)
{
$listItem["Assigned To"].ToString();
$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$listItem["Assigned To"].ToString());
$userfield.User.DisplayName;
Write-Host " Content: " $userfield.User.DisplayName;
$destinationfolder = $tempLocation + "\" + $userfield.User.DisplayName;
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
foreach ($attachment in $listItem.Attachments)
{
$file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
$bytes = $file.OpenBinary()
$path = $destinationfolder + " \" + $attachment
Write " Saving $path"
$fs = new-object System.IO.FileStream($path, " OpenOrCreate")
$fs.Write($bytes, 0 , $bytes.Length)
$fs.Close()
}
}

Wednesday, May 28, 2014

SharePoint 2010 list Column/Field Validation


My customer wanted to be able to make a Column/Field Validation for a SharePoint 2010 List column. Date column(Date1) needs to be validated so that it is greater than 3 days from today but this column is not a required column so it makes little tricky.

 Initially I was using the below simple formula for my column validation,
 =[Date1]>NOW()+3

It works great if there is a value in the Date1 column/field but I ran into an issue when there is no value in this column/field because the validation is triggering. The formula must evaluate to TRUE for validation to pass.

 So I came up with the below formula which uses IF condition,
=IF(Date1]<>"",IF([Date1]>NOW()+3,TRUE,FALSE),TRUE)

If Date1 is not Empty  then I want to make sure that Date1 has a value which is greater than 3 days from today so I go into the second IF statement which will return a TRUE if Date1 is greater than 3 days from today or FALSE if it doesn't match.   If Date1 is empty  or has no value then I don’t care so I will always return TRUE.


If we need to validate 2 fields then we should be using List Validation feature

 

Tuesday, May 27, 2014

PowerShell script to get List name and GUID


Here is a PowerShell script to get the List name along with GUID


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Configure the location for the ListGUID output file
$OutputFile="C:\ListGUID.csv";
#Replace Yoursitename as needed
$site = Get-SPSite http://Yoursitename
$web = $site.RootWeb;
$web.lists | Select-Object title, id | Export-Csv -Path $OutputFile;
$web.Dispose();
$site.Dispose();


 

Wednesday, August 21, 2013

Windows PowerShell Script to get Site Collections Quota in a Farm


We have a requirement to get the list of all the Site collections along with their Site quotas.
Below Windows PowerShell script will output the following information,
 
  • Site URL
  • Assigned/Allocated Site Quota Size
  • Total Storage User
You can copy the below script into a notepad and edit the OutputFile and Siteurl as needed in your environment save the file as Script.PS1. You can also paste the code in the SharePoint 2010 Management Shell. (Keep in mind to run as Administrator)
 

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Configure the location for the SiteCollectionQuota output file
$OutputFile="C:\SiteCollectionsQuota.csv";
"Site URL"+","+"Quota Limit (MB)"+","+"Total Storage Used (MB)"+"" | Out-File -Encoding Default -FilePath $OutputFile;
#Specify the root site collection within the Web app
$Siteurl="Your web application URL";
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;
#Loops through each site collection within the Web app
Foreach ($Site in $Webapp.Sites)
{if ($Site.Quota.Storagemaximumlevel -gt 0) {[int]$MaxStorage=$Site.Quota.StorageMaximumLevel /1MB} else {$MaxStorage="0"};
if ($Site.Usage.Storage -gt 0) {[int]$StorageUsed=$Site.Usage.Storage /1MB};
if ($Storageused-gt 0 -and $Maxstorage-gt 0){[int]$SiteQuotaUsed=$Storageused/$Maxstorage* 100} else {$SiteQuotaUsed="0"};
$Web=$Site.Rootweb; $Site.Url + "," +$MaxStorage+","+$StorageUsed | Out-File -Encoding Default -Append -FilePath $OutputFile;$Site.Dispose()};

 Reference: SharePoint Server 2010 Windows PowerShell cmdlets, http://technet.microsoft.com/en-us/library/ff678226(v=office.14).aspx

Thursday, August 15, 2013

Get SharePoint List Name from GUID


We have couple of errors recorded in ULS logs with List Id\GUID and we need to get the list name to troubleshoot further.

 Here is the code snippet to get SharePoint list Name from GUID.
 
SPSite Site= new SPSite("Your Site Name");
SPWeb Web= Site.OpenWeb();
Guid id = new Guid("Your LIST GUID");
SPList list = Web.Lists.GetList(id, true);
 
Hope this helps