Category Archives: Windows PowerShell ISE

Show or Hide SharePoint List Using CSOM | C# | PowerShell

Disclaimer: This is pretty old topic just remade it using CSOM and PowerShell at my client work place.

During SharePoint due diligence many suggested to use SharePoint Manager Tools, CAML Query Builder etc. It’s better to keep tools handy but gathering and executing the client requirement is better than the best.

Environment: Client has WSS 3.0, MOSS 2007 and SharePoint 2010 platforms, they want to organize the structure and keep the contents similar. An ideal goal is to move to upgraded versions.

During this big discussion development team suggested to make a script which reads the web, site, lists and document library properties and set the same in each farm. Dude! writing script is easy and deploying is very easy as well. But, before we do that let’s know why properties are set differently ? Who made the decision? Business Justification is much more important so I suggested Power Users or SharePoint L1 to decide and deploy the solution.

How? Power Users like GUI based solutions they don’t want to execute console applications or scripts. As usual we picked up a call and I was shocked by listening to content manager requirement – “Need one liner cmdlet and we will create Lists and Set it’s Property. It may be one or many!” It’s that simple PowerShell reached to content managers as well. 🙂 I was happy because I am going to meet their needs in short span of time!

To kick on the demo in a catchy way. I showed them how PowerShell one liner looks like 🙂

Get-EvenNumbers -From 1 -To 10

It’s that simple to use, I haven’ t used any validation in this function very simple code which prints the even number using range operators. The function code is below

Function Get-EvenNumbers
{
    param(
    [Parameter(Mandatory = $true)]
    $From,
    [Parameter(Mandatory=$true)]
    $To
    )

    ($From..$To).ForEach({if($_ %2 -eq 0){$_}})
}
Get-EvenNumbers -From 1 -To 10

Solution for Content Managers:

Consume CSOM and build binary module using Class Library 🙂 🙂 🙂 This time I used Visual Studio 2013 just for a change 🙂

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Net;
using System.Management.Automation;
using Microsoft.SharePoint.Client;

[Cmdlet(VerbsCommon.Set,"SPListVisibility")]
public class SetSPListVisibility : Cmdlet
{
    [Parameter(Mandatory = true)]
    public string SPUrl;

    [Parameter(Mandatory = true)]
    public string SPListName;

    [Parameter(Mandatory = true)]
    public bool ListVisibility;
    protected override void ProcessRecord()
    {
        ClientContext ctx = new ClientContext(SPUrl);
        NetworkCredential credentials = CredentialCache.DefaultNetworkCredentials;
        ctx.Credentials = credentials;

        List list = ctx.Web.Lists.GetByTitle(SPListName);
        ctx.Load(list);

        list.Hidden = ListVisibility;
        list.Update();
        ctx.ExecuteQuery();
        ctx.Dispose();
    }
}

Usage (Just for Demo):

Import-Module C:\Scripts\Module\CSOM_Demo.Dll
#Hide the List
Set-SPListVisibility -SPUrl http://weburl -SPListName "PowerShell" -ListVisibility $false

#Show the List
Set-SPListVisibility -SPUrl http://weburl -SPListName "PowerShell" -ListVisibility $true

In next my next blog I will share the complete module! It’s that’s easy ! Happy SharePointing and Enjoy PowerShell 🙂 🙂 🙂

Reason: Content manager need this kind of solution for re usability. Smart Guy!