Thursday 16 October 2014

The content type “Document Collection Folder” at “…” is sealed.



 The content type “Document Collection Folder” at “…” is sealed - SharePoint 2013


In an Asset Library When user tries to upload a Video file below error pops up 

but this won't pop up if a user tries to upload image files


Solution to Above issue is We need to Activate "Video and Rich Media"  Feature at SiteCollection Level and he has to delete the existing Asset library and create the new Asset library.



Wednesday 15 October 2014

Break List Item Permissions PowerShell


Break Permissions Item Level / Custom Permissions at item level


Blow PowerShell script will retain list permission and give unique permissions atitem level.

We had a requirement to break all item level permissions and remove all the Groups and individual permissions at the item level and Add Owners group and Approvers group  to all items and also it should retain the permissions of item created by.

At the  item level it will delete the all inherited Groups from List and Add specific groups to each item name called " Owners" with  "Full Control " and "Approvers" with "Design" Permissions.



 clear

asnp "*sh*"

$web=Get-SPWeb -Identity "http://weshare.sedwdev.local/sites/teste"

Write-Host " Author is " $web.Author.DisplayName

$list=$web.Lists.TryGetList("SourceList")

foreach($item in $list.Items)
{
    if(!$item.HasUniqueRoleAssignments)
 {
        $item.BreakRoleInheritance($false)

 for($i=0 ; $i -lt $item.RoleAssignments.Count;$i++)
    {

   if(!$item["Created By"])
{
     $item.RoleAssignments.remove($i)

    $item.Title + "permissions Removed "
  
  } }

  foreach( $grp in $web.Groups)
  {
    
     if ($grp.Name -match "teste Owners")
     {

     $rolAsgnmt=New-Object Microsoft.SharePoint.SPRoleAssignment($grp);

     $roldef=$web.RoleDefinitions["Full Control"]

     $rolAsgnmt.RoleDefinitionBindings.Add($roldef);

     $item.RoleAssignments.Add($rolAsgnmt);

     " Item title is  "+ $item.Title + "  Group Added is  " + $grp.Name

     $item.Update();
    
     }

    elseif ($grp.Name -match "Approvers")
     {

     $rolAsgnmt1=New-Object Microsoft.SharePoint.SPRoleAssignment($grp);

     $roldef1=$web.RoleDefinitions["Design"]

     $rolAsgnmt1.RoleDefinitionBindings.Add($roldef1);

     $item.RoleAssignments.Add($rolAsgnmt1);

     " Item title is  "+ $item.Title + "  Group Added is  " + $grp.Name

     $item.Update();
     }
     }  
 }
 }

OutPut:





Wednesday 8 October 2014

Remove Duplicates in Search Results Webpart


Remove duplicates in Search Results Webpart


asnp "*sh*"


$resultsPage = "Pages/default.aspx"

$resultsUrl = "$siteUrl/$resultsPage"

$spweb = Get-SPWeb $siteUrl

$page = $spweb.GetFile($resultsPage)

# Check if the page is checked out to someone if so override the checkout

$bool=$page.CheckedOut

if($bool)
{
$page.UndoCheckOut();
}

# checkout the Page

$page.CheckOut()

# Get the web part manager for the page
$webPartManager = $spweb.GetLimitedWebPartManager($resultsUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

# And pull the web part we want to work on
$webpart = $webpartmanager.WebParts | ? { $_.Title -eq 'Latest News' }

$dataProvider = ConvertFrom-Json $webpart.DataProviderJSON

$dataProvider.TrimDuplicates = 'True'

$webpart.DataProviderJSON = ConvertTo-Json $dataProvider -Compress

$webpartmanager.SaveChanges($webpart)

$page.CheckIn('Setting the duplicates in LatestNews');

$page.Publish('Duplicates were set in Latest News Webpart');

$spweb.Dispose();


OutPut:


Tuesday 7 October 2014

Increase Site Quota using PowerShell

PowerShell Script to Increase Storage Individual Quota


Increasing the SiteCollection Storage without Specifying Site Quota that is individual level,

Here we are increasing the site quota to 5 GB, and setting Warning to 4.5 GB



clear

asnp "*sh*"

$site=Get-SPSite -Identity "http://sedwdevrtm:432/sites/ProductsSite"

# Here We are setting Max storage to 5GB and Warning level to 4GB

Set-SPSite -Identity $site -MaxSize 5GB -WarningSize 4.75GB 

# we are seeing what is the maximum storage and warning level set for the site.

$site.Quota.StorageMaximumLevel/1GB


$site.Quota.StorageWarningLevel/1GB  


Output:




Monday 4 August 2014

Rename a Site using Powershell/ Rename SiteCollection Powershell


PowerShell Script to rename a SiteCollection

Here are renaming the site from energimiljo to Energymill

cls

asnp "*sh*"

$site=Get-SPSite -Identity "http://SP2013dev.com/sites/energimiljo/"

$site.Rename("http://SP2013dev.com/sites/energymills")

$site.Dispose()

Tuesday 29 July 2014

Add WebPart to Page using PowerShell


Below Powershell Script will add Webpart to a page , by reading the Site URL from text file , and 

here we are adding the Webpart to a page by exporting the webpart to our local drive and exported 

webpart will be in xml file and we are writing the xml file to a page.

To add a webpart to a page first we need to check if a page is checked out to any user, if so then act according to your requirement, here in our case we are overriding the checkout and adding the webpart and publishing the same to the page.

Here we are writing the output to textfile instead of writing it on powershell console.

Here "CollabrationURL" is the name of the column in the textfile for SiteURL

cls

asnp "*sh*"

$file=Import-Csv -Path "C:\Manju\SiteUrl.txt"

$web=Get-SPweb -Identity $file.CollabrationURL

[xml]$webpartxml= Get-Content -Path "C:\Manju\WPRequest.xml"

$SR = New-Object System.IO.StringReader($webpartxml.OuterXml)

$XTR = New-Object System.Xml.XmlTextReader($SR)

$err=$null

$WebPartZoneID = "Topzone"

$WebPartZoneIndex = 0

 try
   {

  $page=$web.GetFile("Pages/default.aspx");

  $bool=$page.CheckedOutBy

        if($bool)
        {
            Write-Host "Page is already Checkout to " $page.CheckedOutBy.UserLogin

            $page.UndoCheckOut()

            Write-Host "Page is Over ridded by " $web.CurrentUser.DisplayName
        }

    $page.CheckOut();

    $wmgr=$web.GetLimitedWebPartManager($page,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

    $webpart=$wmgr.ImportWebPart($XTR,[ref]$err);

    $wmgr.AddWebPart($webpart,$WebPartZoneID,$WebPartZoneIndex);

    $page.CheckIn('dude');

    $page.Publish('Adding request Site Webpart')

    "Request Site WebPart SucessfullAdded" + (Get-Date -DisplayHint Date) | Out-File -Append "C:\OutPutLog.txt"
    
     $SR.Close();
     $XTR.Close();
     $web.Dispose()

    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
   
        "Request Site WebPart Failure" + $ErrorMessage  +  (Get-Date -DisplayHint Date) | Out-File -Append "C:\ErrorLog.txt"
    }




Thursday 24 July 2014

Add a file to a document library using PowerShell / Upload Files to Document Library Using Powershell


Add files in a Folder to a Document Library using PowerShell

Below Powershell Script will upload the files in your Local Drive to SharePoint Document library.

Here i am Uploading to Library named Documents from D drive , 
cls

asnp "*sh*"

$url=Read-Host "Enter Site Url"

$web=Get-SPWeb -Identity $url

if($web)
{
    try
    {
    $list = $web.Lists.TryGetList("Documents")

    $files = Get-ChildItem -Path "D:\Manju" -Force -Recurse

        foreach ($file in $files)
        {
          $stream = $file.OpenRead()

          $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)
          
          Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green         

        }
    }
    catch
    {
    $ErrorMessage = $_.Exception.Message
    Write-Host $ErrorMessage -BackgroundColor Yellow 
    }
  }

  else
  {
  Write-Host "Site Doesn't exist"
  }

$list.Update();







Friday 4 July 2014

Find UserProfile using PowerShell /Find User in UserProfile Using PowerShell Script


In the below Powershell script We are trying to find an user from UserProfile


clear

asnp "*sh*"

$site=new-object Microsoft.SharePoint.SPSite("http://SP2013dev.local/");    
      
$serviceContext = Get-SPServiceContext $site;
          
$site.Dispose();     
     
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);

$userProfile = $upm.GetUserProfile("Manjunath");

$userProfile.AccountName



OutPut:



Wednesday 2 July 2014

Collection was modified; enumeration operation may not execute Powershell


 “Collection was modified; enumeration operation may not execute,

 Occurs in  foreach loops while you are enumerating through the loop and trying to modify items.

Which modifys the first item in the loop and throws this error without modifying the rest of the items.


Generally it occurs if you try and modify a collection’s references while using foreach to enumerate through it.
For example:

I  am trying to remove all the Supported UI Languages ,by retaining only site created in default site Locale and english locale.


    if($web.IsMultilingual -eq $true  )

      {

        foreach($cul in $web.SupportedUICultures)
       {
         if($cul.LCID -ne  $webCul.LCID -and $cul.LCID -ne "1033")
         {    

           $web.RemoveSupportedUICulture($cul)
          
        
          }
       $web.Update()
       }
    }


for the first time it will go through the loop foreach will remove supported culture for frist time, when it comes to loop for the second iteration then it will throw you the exception  Collection was modified; enumeration operation may not execute,

Solution to Above problem is to Store to values to be modified in a Arraylist and try to modify which will fix the problem, Here i am storing  Arraylist called enumcul and inserting values into it and modifying it...

  
  $enumcul=New-Object Collections.ArrayList
    $i=0
    if($web.IsMultilingual -eq $true  )
      {

        foreach($cul in $web.SupportedUICultures)
       {
         if($cul.LCID -ne  $webCul.LCID -and $cul.LCID -ne "1033")
         {

          $enumcul.Insert($i, $cul)
          $i=$i+1
          }
     
       }

      
     foreach$k in $enumcul)
     {
     
        $web.RemoveSupportedUICulture($k)
        $web.Update()
     }