Wednesday, November 2, 2011

Display all SharePoint portal list and Library on Powershell Console.

Leave a Comment
Execute following script in power shell and you will get list of all lists and libraries in SPWeb

PS> $site = Get-SPSite("Site URL");
        $web = $site.OpenWeb();
        $lists = $web.Lists;
        $lists | ForEach-Object { Write-Host $_.Title -Foregoundcolor red };

 // Foregoundcolor red : To show output in red.

Get User Name from User Information List using PowerShell

Leave a Comment
Execute following script in Power Shell and get value of Name Field from User Information List

PS > $site = Get-SPSite("Site URL");
         $web = $site.OpenWeb( );
         $list =  $web.Lists["User Information List"]

          // Here i am getting item by id but we can also use SPQuery object to get item from user information list and get SPListItem object.

         $listitem = $list.GetItemByID(1);
         Write-Host  $listitem["Name"]; 

Sunday, October 30, 2011

Create Document Library in sharepoint site Using Powershell

Leave a Comment
Execute following script in powershell to create document library in SharePoint site.

PS > $site = Get-SPSite("Site URL"); 
         $web =  $site.OpenWeb( );
         $template = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary;
         $web.Lists.Add("TestDocumentLibrary","Document Library Description",$template);
         $web.Update( );

Cheers !


Delete List from SharePoint Portal through PowerShell

Leave a Comment
Execute following script in powershell to delete list from SharePoint Portal

PS> $site = Get-SPSite("Site URL");
        $web = $web.OpenWeb( );
        $list = $web.Lists["ListName"];
        $list.Delete( );
        $web.Update( );

Press Enter and List specified here will be deleted from SharePoint list.

Cheers !

Friday, October 28, 2011

Create SPList through Powershell

Leave a Comment
Scenario : Create List in SharePoint through PowerShell

Execute following script in PowerShell

PS > $site = Get-SPSite("SPSiteUrl")
PS > $web = $site.OpenWeb()
PS > $listcollection = $web.Lists
PS > $template = [Microsoft.SharePoint.SPListTemplateType]::GenericList
PS > $newid = $listcollection.Add("MYList","MyList Description",$template);
PS > $newlist = $web.Lists["MYList"];
PS > $newlist.OnQuickLaunch = "True"
PS > $newlist.Update();

Thursday, October 27, 2011

Powershell : Get-SPSite throws not recognized error


Scenario : Get SPSite object in PowerShell 

While initializing SPSite objet in powershell, I got this error :

 The term ‘Get-SPSite’ is not recognized as the name of cmdlet, function, script file, or operable program

To resolve this issue fire this in powershell.

Add-PSSnapin Microsoft.SharePoint.Powershell

Check article here , to find more information on Add-PSSnapin.

Get value of Choice Field from SharePoint List

Leave a Comment

Scenario:  Get value from Choice Field of SharePoint list in windows console application

SPSite site = new SPSite("Full qualified Site Url”);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["ListName"];

// Here it is assumed that you know item id. But you can use SPQuery object to get specific item from SharePoint list.
SPListItem item = list.GetItemById(1);

string value = item.GetFormattedValue("MyColumn");



Tuesday, October 25, 2011

Show User “I Like It” data in Webpart

Leave a Comment

"I like it" data is referred using Microsoft.Office.Server.SocialData Namespace.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;
using Microsoft.Office.Server.SocialData;
using System.Text;
namespace ResearchProject.WebParts
    public class LikeWebpart : WebPart
        protected override void CreateChildControls()
            UserProfile profile = null;
            SPServiceContext serviceContext = SPServiceContext.Current;
            UserProfileManager profileManager = new UserProfileManager(serviceContext);

            profile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.RawSid);

            SocialTagManager socialmanager = new SocialTagManager(serviceContext);
            SocialTag[] tags = socialmanager.GetTags(profile);

            StringBuilder tagstring = new StringBuilder();
            foreach (SocialTag tag in tags)
                if (tagstring.Length > 0)
            this.Controls.Add(new LiteralControl(tagstring.ToString()));