Wednesday, 27 May 2015

Reserved querystring parameter in Sharepoint

Below querystring parameters are reserved in SharePoint and using them may give unpredictable results.

  • FeatureId
  • ListTemplate
  • List
  • ID
  • VersionNo
  • ContentTypeId
  • RootFolder
  • View
  • FolderCTID
  • Mode
  • Type
  • PageVersion
  • IsDlg
  • Title
  • _V3List_

To ensure that the creation of the SPContext object does not fail your application should ensure that your query string parameters are different from those listed above.

Reference: http://blogs.technet.com/b/stefan_gossner/archive/2009/01/30/querystring-parameters-you-should-not-use-in-your-sharepoint-application.aspx

Tuesday, 26 May 2015

How to exclude or skip part of page content from SharePoint search crawler

To do this we have to use the "noindex" class, like this,

<div class="noindex"> footer </div>

This will be helpful if you want to avoid footer to get crawled in search. Because of the fact that the footer is part of the master page the crawler may index every single page that inherits from the master page. This will cause misleading and wrong search results for the visitors of the site.

Monday, 25 May 2015

How to change default tile size in SharePoint 2013

Using a CSS can be approach to achieve this.

Add the following CSS inside the content editor webpart or add it to your custom css

<style type="text/css">
            div.ms-promlink-body {
                height: 100px;
            }

            div.ms-tileview-tile-root {
                height: 110px !important;
                width: 110px !important;
            }

            div.ms-tileview-tile-content, div.ms-tileview-tile-detailsBox,  div.ms-tileview-tile-content > a > div > span {
                height: 100px !important;
                width: 100px !important;
            }

            div.ms-tileview-tile-content > a > div > img {
                max-width: 100%;
                width: 100% !important;
            }

            ul.ms-tileview-tile-detailsListMedium {
                height: 100px;
                padding: 0;
            }

            li.ms-tileview-tile-descriptionMedium {
                font-size: 11px;
                line-height: 16px;
            }

            div.ms-tileview-tile-titleTextMediumExpanded, div.ms-tileview-tile-titleTextLargeCollapsed, div.ms-tileview-tile-titleTextLargeExpanded {
                padding: 3px;
            }

            div.ms-tileview-tile-titleTextMediumCollapsed {
                background: none repeat scroll 0 0 #002E4F;
                font-size: 12px;
                line-height: 16px;
                min-height: 36px;
                min-width: 97px;
                padding-left: 3px;
                position: absolute;
                top: -36px;
            }

            li.ms-tileview-tile-descriptionMedium {
                font-size: 11px;
                line-height: 14px;
                padding: 3px;
            }

</style>


Hope this helps..!!!

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.!!!!!!!

Change linked to item with edit menu to different column

Normally a SharePoint list links to an item using the “Title” field. But in some cases you want to hide Title Column and and add linked to item link to another column.

Now to link the Item to a specific column/field, you need to open the list in SharePoint designer and look for the <viewfields> tags.
And then add LinkToItem="TRUE" to whichever column you want the link.

 <ViewFields>  
   <FieldRef Name="field1"/><br/>  
   <FieldRef Name="field2"/><br/>  
   <FieldRef Name="field3" LinkToItem="TRUE"/><br/>  
   <FieldRef Name="field4"/><br/>  
   <FieldRef Name="field5"/><br/>  
 </ViewFields>  

Save and you are done.!!!!!!

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!!!

How to access SharePoint server “Outgoing E-Mail Settings” from code?

Outgoing email setting are configured in central administration at path:

Central Admin -> System Settings -> Configure outgoing e-mail settings

Below is the code to access outgoing e-mail settings using object model:

//Get the site ID
Guid siteID = SPContext.Current.Site.ID;
protected string GetSMTPHostName()
{
    using (SPSite site = new SPSite(siteID))
    {
      //Get the SMTP host name from “Outgoing e-mail settings”
      return site.WebApplication.OutboundMailServiceInstance.Parent.Name;
    }
}
protected string GetFromEmailID()
{
    using (SPSite site = new SPSite(siteID))
    {
      //Get the “from email address” from “Outgoing e-mail settings”
      return site.WebApplication.OutboundMailSenderAddress;

    }
}

Namespace: Microsoft.SharePoint.Administration
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)

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...