PowerShell and SharePoint: Getting to Know Your Site Collection -- Hidden Lists and Libraries

 In this part of the series, I have so much to cover that I am just going to dive in. Are you ready? Good!

But first:

Let's take a quick look at the code that connected us to SharePoint. If you remember, in our last article, we simply connected to SharePoint and got the .aspx home page. Now, yes, it didn't look like any HTML page we have ever seen but if you did your homework and played with the $Repsonse variable, you would have discovered that the .aspx is contained within the $Reponse variable.

Clear-Host
$Response = Invoke-RestMethod -Method Get -uri "http://i.run.com/" -Credential "<my credentials>"

In this article I want to know my Site Collection. I want to know things about it. I don't necessarily want to write back to SharePoint, I just want to discover things, properties about it. So let's dig into knowing our Site Collection. To do that, I am going to change the -Uri parameter to point to a REST endpoint. Once we do that we can then start interacting with SharePoint like we had hoped for in the beginning.

Clear-Host
#
#gets user credentials
Function Get-MyCredentials(){
    if(!$creds){
        $creds = Get-Credential
    }
    return $creds
}

$Response = Invoke-RestMethod -Method Get -uri "http://i.run.com/_api/web/lists/" -Credential (Get-MyCredentials)

I have added a few more lines to my code:

  • Function Get-MyCredentials: is a simple function that will prompt me for the credentials I want to use to authenticate to SharePoint. I call this function every time I want to make a connection to SharePoint but it will only prompt me once per PowerShell session. The variable $creds is then passed to the -Credentials parameter which will then be used to authenticate.
  • -Uri: the uri parameter has also changed. What I am asking from SharePoint in the new Uri is to give me all the list and document libraries in the Site Collection.

When I run that code and type $Response in the terminal window I get the following:

...and that is only a small part of the results. Your results may vary depending on the amount of list and libraries in your site. Since I am in discovery mode and want to get to know my Site Collection just a tad better let's look at the results a bit closer.

$Response.getType()

Notice this is not xml like the first time we ran .getType(). This time, we have an array. Granted, this is an array of xml but the point here is that you always want to know what you are working with so you can know how to get the information that you want/need out.

With this being an array, le's find out how many list/libraries we have in our Site Collection:

$Response.length

According to my results I have 22 lists/libraries in my Site Collection. Should we compare with the SharePoint UI/All Site Contents? *nod* *nod*

Woa! I am off by quite a few. My PowerShell script tells me I have 22 lists/libraries yet the UI is telling me I have but 9. Something is not right here. Since this article is about discovery, let's go figure out why my numbers do not match.

When I typed $Response in the terminal window I got a list of items.

From this list we can get some information like the GUID of a list. Which list, well we don't particularly know. For us to find out we can either get that id property ($Response.id), save it to a variable, and then make another REST call with the id in the Uri. We go go through all of that because, thankfully we have another property here called 'content'. Content has all the information we need.

$Response.content

Well, that doesn't look very helpful but remember this is an xml document. What this screen is telling us is that we are still viewing an xml document and 'properties' is a node in the xml document. So let's go see.

$Response.content.properties

Now this is more like it! I only captured the last list item in the result. Remember, the length of the $Response array was 22. The screen shot above is of item number 21, the last one in the array with an array being 0 based. This screen shot has a lot of information. Let's dig in some more

$Response.content.properties.title

All of our list Titles!

$Response.content.properties.ItemCount

How many items each list/library has. How else would you get this information in one quick swoop? Imagine having 50 list/libraries, 100 of them. This could save you some time.

One thing still remains though, we have not answered the question from above. The $Respone array has 22 items and the SharePoint UI only shows 9. Why?

Notice the property "Hidden". SharePoint hides lists/libraries from users that belong to SharePoint. They are like your hidden files and folders in the Window OS. So, what would happen If we iterate through the list and libraries and check the value of the "Hidden" property?

for($i = 0; $i -lt $Response.Length; $i++){
    if($Response[$i].content.properties.Hidden.'#text' -eq $false){
        $Response.content.properties[$i].title
    }
}

Pretty cool, eh? Now my total list/libraries match the SharePoint UI numbers AND I know that I have some List and Libraries in my Site Collection that are hidden. To see those List and Libraries, I simply take the For loop above and change the boolean to $true and run it again.

Some of these list should look familiar: Solution Gallery, Master Page Gallery to name a few but if they don't, well that is fine. 

Comments