Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Monday, 19 June 2017

Powershell to get display name and internal name of SharePoint list columns

Below is the Powershell script to get column information from SharePoint list to .csv file.

$url = "Your SharePoint Site URL"
$listName = "List Name"
$path ="c:\Columns.csv"

$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$list.Fields | select Title, InternalName |  Export-Csv -path $path

Hope this helps!!!!

Saturday, 17 December 2016

Set All lists in SharePoint Site to open in dialog

Recently in one of the project I needed to set all list in site to open in dialog. As it was more than 40 lists it was tedious job to do one by one.

A quick search on google gave me below powershell command which helped me to achive the functionality.

$web = Get-SPWeb -Identity http://rootsite/subsite
foreach ($list in $web.Lists)  { $list.NavigateForFormsPages = $false; $list.Update(); }




where false indicates that the list form page is launched in a modal dialog.

Hope this helps

How to restore deleted OOB timer job in SharePoint?

The simple way which I found and worked in my case is, to running below PowerShell script.

The PowerShell script restores the deleted timer job,

$farm = Get-SPFarm  
$farm.TimerService.EnsureDefaultJobs()  
Reference: http://sharepoint.stackexchange.com/questions/137538/how-to-restore-deleted-oob-timer-job

Friday, 26 June 2015

Publishing Feature activation failed. Exception: System.ArgumentException: Value does not fall within the expected range.

Today while activating publishing feature in Site Settings I was getting an error. When I checked log file using correlation id , it was showing unexpected error as:

Publishing Feature activation failed. Exception: System.ArgumentException: Value does not fall within the expected range.

Then i tried to activate feature using Powershell and it worked:

$siteUrl = "http://SharePoint:6435/"
$siteCollection = Get-SPSite $siteUrl
Enable-SPFeature "PublishingSite" -Url $siteCollection.Url -force
Enable-SPFeature "PublishingWeb" -Url $siteCollection.Url -force

Hope this helps.!!!!!!!

Monday, 25 May 2015

How to find all document of particular content type

We can use below powershell to find documents of certain content type:

 $site = Get-SPSite("your-site-url");  
 foreach ($web in $site.AllWebs) {  
   $ctype = $web.ContentTypes["Your Content Type"]  
   $usages = [Microsoft.Sharepoint.SPContentTypeUsage]::GetUsages($ctype)  
   foreach ($usage in $usages) {  
    Write-Host $usage.Url  
   }  



To find it using code you can below lines of code

 using (SPSite siteCollection = new SPSite("http://localhost"))  
      {  
       using (SPWeb webSite = siteCollection.OpenWeb())  
       {  
         // Get the content type.  
         SPContentType obsolete = webSite.ContentTypes["Test"]; 
 
         // We have a content type.  
         if (obsolete != null)   
         {  
          IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);  
          // It is in use.  
          if (usages.Count > 0)   
          {  
            Console.WriteLine("The content type is in use in the following locations:");  
            foreach (SPContentTypeUsage usage in usages)  
             Console.WriteLine(usage.Url);  
          }            
         }         
         else   
         {  
          Console.WriteLine("The content type does not exist in this site collection.");  
         }  
       }  
      }  
      Console.Write("\nPress ENTER to continue...");  
      Console.ReadLine();  

Hope this helps.!!!!!!!

Wednesday, 6 May 2015

Upload file into SharePoint doument library with metadata using PowerShell

In this post we will see how to upload the file into the document library and insert/update the metadata columns. I am having 1 people picker data types/user group columns and 1 single line of text columns.

Below is the PowerShell to upload documents in PowerShell and update the metadata:


[System.Reflection.Assembly]::LoadWithPartialName 
        ("Microsoft.SharePoint")
        if((Get-PSSnapin | Where {$_.Name -
        eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
            Add-PSSnapin Microsoft.SharePoint.PowerShell;
              }

           #Site Collection where you want to upload files
          $siteCollUrl = "YourSiteURL"
          #Document Library where you want to upload files
         $libraryName = "DocLibName"
         #Physical/Network location of files
          $reportFilesLocation  = "D:\FileUpload"

        $spSourceWeb = Get-SPWeb $siteCollUrl;
        $spSourceList = $spSourceWeb.Lists[$libraryName];

            if($spSourceList -eq $null)
            {
           Write-Host "The Library $libraryName could not be found."
            return;
            }

   $files = ([System.IO.DirectoryInfo] (Get-Item 
    $reportFilesLocation)).GetFiles()
  foreach($file in $files)
  {
    #Open file
   $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

   #Add file
   $folder =  $spSourceWeb.getfolder($libraryName)

   Write-Host "Copying file $file to $libraryName..."
   $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,   
   [System.IO.Stream]$fileStream, $true)

$item = $spFile.Item;
$item["TextOrChoiceColumnInternalName"] = "New Value"
$spUser = Get-SPUser -Identity "domain\username" -Web $spSourceWeb;
$item["UserFieldInternalName"] = $spUser;
$item.SystemUpdate($false);
#Use SystemUpdate($false) if you don't want to increase the version of the item..

   #Close file stream
  $fileStream.Close();
 }
$spSourceWeb.dispose();
Write-Host "Files have been uploaded to $libraryName."


Using this PowerShell script you can upload documents with metadata.

To check in a file use:
$spFile.CheckIn("Checked In By Administrator")

To publish a file:
$spFile.Publish("Automatically published by Powershell")

To approve a file:
$spFile.Approve("Automatically approved by by Powershell");

Hope this helps!!!

Tuesday, 5 May 2015

How to start the workflow for an item using PowerShell / C#

In this article we will be seeing how to start a workflow for an item using c# and powershell

Start a workflow using c#

class Program    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("YourSiteURL"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists["Test"]; 
                    SPListItem item=list.Items[0];
                    SPWorkflowManager manager = site.WorkflowManager;
                    SPWorkflowAssociation association = list.WorkflowAssociations[0];
                    string data = association.AssociationData;
                    SPWorkflow wf = manager.StartWorkflow(item, association, data, true);
                }
            }
        }
    }


Start a workflow using powershell:

$siteURL="http://servername:1111/"
$listName="Test"
$site=Get-Spsite $siteURL
$web=$site.RootWeb
$list=$web.Lists[$listName]
$item=$list.Items[0]
$manager=$site.WorkFlowManager
$association=$list.WorkFlowAssociations[0]
$data=$association.AssociationData
$wf=$manager.StartWorkFlow($item,$association,$data,$true)

Hope this helps!!!!

How to get document library size using PowerShell?

Below is the PowerShell script which will show the size of Particular library:

   [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0,             Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0,   Culture=neutral, PublicKeyToken=71e9bce111e9429c”)


    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

    $siteURL = "YourSiteURL" 
    $site = new-object Microsoft.SharePoint.SPSite($siteURL)


    foreach ($web in $site.AllWebs)
    {
     foreach ($list in $web.Lists)
    {

            if($list.BaseType -eq "DocumentLibrary")   
          {

if($list.Title -eq "YourDocumentLibraryTitle")
{
        $listSize = 0
       foreach ($item in $list.items) 
            { 
              $listSize += ($item.file).length
            }
         "Web: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB"     
}
    }
    }
    }


Hope this helps!!!!

Related Posts Plugin for WordPress, Blogger...