SharePoint Pals
 | Sign In
How to Retrieve Basic SharePoint List Information – PowerShell
by Sathish Nadarajan 4 Jun 2016
Author
SharePoint MVP
Visits   
Today  :   5     Total  :    3259

In this article, let us see, how to retrieve the basic information of a List in a SharePoint Site.

 
 ####### This method will retrieve the Basic list Info  ##################
 
 function GetListInfo()
 {
     Write-Host "Entered GetListInfo Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetListInfo Method"
     $script:Progress = "8:Entered"
     
     # Get WebApplication Object
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $ListInfo_XML_Path = $scriptBase + "\Reports-$LogTime\ListInfo.xml"
     
     # Create the XML File Tags
     
     $xmlWriter = New-Object System.XMl.XmlTextWriter($ListInfo_XML_Path,$Null)
     $xmlWriter.Formatting = 'Indented'
     $xmlWriter.Indentation = 1
     $XmlWriter.IndentChar = "`t"
     $xmlWriter.WriteStartDocument()
     $xmlWriter.WriteComment('Web Information' + $WebApplication.DisplayName)
     $xmlWriter.WriteStartElement('WebApplication')
     $xmlWriter.WriteEndElement()
     $xmlWriter.WriteEndDocument()
     $xmlWriter.Flush()
     $xmlWriter.Close()
     
     
     # Get the Site Collections 
     $SiteCollections =  $WebApplication | Get-SPSite -Limit All
     #$SiteCollections =  Get-SPSite "http://ctsc00556722901:5555/sites/TeamSite2/"
     
     # write the output on XML File
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $ListInfo_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($ListInfo_XML_Path)
     
     # iterate through the Site Collection
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         $Webs = Get-SPWeb -site $SiteCollection -Limit All 
         
         # write the output on XML File
         $xmlDoc = [System.Xml.XmlDocument](Get-Content $ListInfo_XML_Path);
         $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
         $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
         $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
         
         $subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
         $subSitesNode.SetAttribute("Count", $Webs.Count)
         $xmlDoc.Save($ListInfo_XML_Path)
         
         # Iterate through the Webs    
         foreach($Web in $Webs)
         {
             $subSiteNameNode = $subSitesNode.AppendChild($xmlDoc.CreateElement("SubSite"));
             $subSiteNameNode.SetAttribute("Title", $Web.Title)
             $subSiteNameNode.SetAttribute("URL", $Web.Url)
             $subSiteNameNode.SetAttribute("WebID", $Web.Id)
             
             $parentWebTitle = ""
                             
             if($Web.ParentWebID -ne "00000000-0000-0000-0000-000000000000")
             {
                 $parentWeb = $SiteCollection.OpenWeb($Web.ParentWebID)
                 $parentWebTitle = $parentWeb.Title
                 
             }
             else
             {
                 $parentWebTitle = "RootWeb"
             }    
             $subSiteNameNode.SetAttribute("ParentWebName", $parentWebTitle)
             $subSiteNameNode.SetAttribute("ParentWebID", $Web.ParentWebID)
             
             # Get the Lists    
             $Lists = $Web.Lists
             $ListsElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Lists"));
             
             # Iterate through the Lists
             foreach($List in $Lists)
             {
                 $ListElement = $ListsElement.AppendChild($xmlDoc.CreateElement("List"));
                 $ListElement.SetAttribute("Title", $List.Title)
                 
                 if($Config.Configuration.ListInfo.RootFolder -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $RootFolderElement = $ListElement.AppendChild($xmlDoc.CreateElement("RootFolder"));
                     $RootFolderTextNode = $RootFolderElement.AppendChild($xmlDoc.CreateTextNode($List.RootFolder));
                 }
                 if($Config.Configuration.ListInfo.BaseType -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $BaseTypeElement = $ListElement.AppendChild($xmlDoc.CreateElement("BaseType"));
                     $BaseTypeTextNode = $BaseTypeElement.AppendChild($xmlDoc.CreateTextNode($List.BaseType));
                 }
                 if($Config.Configuration.ListInfo.BaseTemplate -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $BaseTemplateElement = $ListElement.AppendChild($xmlDoc.CreateElement("BaseTemplate"));
                     $BaseTemplateTextNode = $BaseTemplateElement.AppendChild($xmlDoc.CreateTextNode($List.BaseTemplate));
                 }
                 if($Config.Configuration.ListInfo.IsHidden -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $HiddenElement = $ListElement.AppendChild($xmlDoc.CreateElement("IsHidden"));
                     $HiddenTextNode = $HiddenElement.AppendChild($xmlDoc.CreateTextNode($List.Hidden));
                 }
                 if($Config.Configuration.ListInfo.IsThrottled -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $IsThrottledElement = $ListElement.AppendChild($xmlDoc.CreateElement("IsThrottled"));
                     $IsThrottledTextNode = $IsThrottledElement.AppendChild($xmlDoc.CreateTextNode($List.IsThrottled));
                 }
                 if($Config.Configuration.ListInfo.IsInfoPathUsed -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $IsInfoPathUsed = IsInfoPathUsed($List)
                     $IsInfoPathUsedElement = $ListElement.AppendChild($xmlDoc.CreateElement("IsInfoPathUsed"));
                     $IsInfoPathUsedTextNode = $IsInfoPathUsedElement.AppendChild($xmlDoc.CreateTextNode($IsInfoPathUsed));
                 }
                 if($Config.Configuration.ListInfo.LastModified -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $LastModifiedElement = $ListElement.AppendChild($xmlDoc.CreateElement("LastModified"));
                     $LastModifiedTextNode = $LastModifiedElement.AppendChild($xmlDoc.CreateTextNode($List.LastItemModifiedDate));
                 }
                 if($Config.Configuration.ListInfo.ListItemsCount -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     $ListItemsCountElement = $ListElement.AppendChild($xmlDoc.CreateElement("ListItemsCount"));
                     $ListItemsCountTextNode = $ListItemsCountElement.AppendChild($xmlDoc.CreateTextNode($List.ItemCount));
                 }
                 if($Config.Configuration.ListInfo.Fields -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     ## Field Information Starts
                     $FieldsElement = $ListElement.AppendChild($xmlDoc.CreateElement("Fields"));
                     foreach($Field in $List.Fields)
                     {
                         $FieldElement = $FieldsElement.AppendChild($xmlDoc.CreateElement("Field"));
                         $FieldElement.SetAttribute("DisplayName", $Field)
                         $FieldElement.SetAttribute("InternalName", $Field.InternalName)
                         $FieldElement.SetAttribute("Group", $Field.Group)
                         $FieldElement.SetAttribute("Type", $Field.Type)
                         $FieldElement.SetAttribute("Id", $Field.Id)
                         
                         $IsLookUp = "False";
                         
                         If($Field.TypeAsString -eq "Lookup")
                         {
                             #if($Field.Title -eq "LookupTitle")
                             #{
                                 $IsLookUp = "True";
                                 $IsLookUpElement = $FieldElement.AppendChild($xmlDoc.CreateElement("IsLookUp"));
                                 $IsLookUpElement.SetAttribute("LookUp", $IsLookUp) 
                                 
                                 if($Field.LookupList -ne $null -and $Field.LookupList -ne "")
                                 {
                                     try
                                     {
                                         $listGuid = [Guid]$Field.LookupList
                                         $LookUpParentList = $Lists.GetList($Field.LookupList, $False)
                                         
                                         $LookupParentListElement = $IsLookUpElement.AppendChild($xmlDoc.CreateElement("LookUpParentList"));
                                         $LookupParentListTextNode = $LookupParentListElement.AppendChild($xmlDoc.CreateTextNode($LookUpParentList));
                                         $LookupFieldElement = $IsLookUpElement.AppendChild($xmlDoc.CreateElement("LookUpField"));
                                         $LookupFieldTextNode = $LookupFieldElement.AppendChild($xmlDoc.CreateTextNode($Field.LookupField));
                                     }
                                     catch
                                     {
                                         $LookupParentListElement = $IsLookUpElement.AppendChild($xmlDoc.CreateElement("LookUpParentList"));
                                         $LookupParentListTextNode = $LookupParentListElement.AppendChild($xmlDoc.CreateTextNode($Field.LookupList));
                                         $LookupFieldElement = $IsLookUpElement.AppendChild($xmlDoc.CreateElement("LookUpField"));
                                         $LookupFieldTextNode = $LookupFieldElement.AppendChild($xmlDoc.CreateTextNode($Field.LookupField));
                                     }
                                 }
                             #}
                         }
                         else
                         {
                             $IsLookUpElement = $FieldElement.AppendChild($xmlDoc.CreateElement("IsLookUp"));
                             $IsLookUpElement.SetAttribute("LookUp", $IsLookUp) 
                         }
                     }
                     ## Field Information Ends
                 }
                 
                 if($Config.Configuration.ListInfo.ContentTypes -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     ### Content Types
                     
                     $ContentTypesElement = $ListElement.AppendChild($xmlDoc.CreateElement("ContentTypes"));
                     $ContentTypesElement.SetAttribute("Count",$List.ContentTypes.Count);
                     
                     foreach($ContentType in $List.ContentTypes)
                     {
                         $ContentTypeElement = $ContentTypesElement.AppendChild($xmlDoc.CreateElement("ContentType"));
                         $ContentTypeElement.SetAttribute("Name", $ContentType.Name) 
                         $ContentTypeElement.SetAttribute("Id", $ContentType.Id) 
                         
                         $ContentTypeFieldsElement = $ContentTypeElement.AppendChild($xmlDoc.CreateElement("Fields"));
                         foreach($ContentTypeField in $ContentType.Fields)
                         {
                             $ContentTypeFieldElement = $ContentTypeFieldsElement.AppendChild($xmlDoc.CreateElement("Field"));
                             $ContentTypeFieldElement.SetAttribute("DisplayName", $ContentTypeField) 
                             $ContentTypeFieldElement.SetAttribute("InternalName", $ContentTypeField.InternalName) 
                             $ContentTypeFieldElement.SetAttribute("Id", $ContentTypeField.Id) 
                             $ContentTypeFieldElement.SetAttribute("Type", $ContentTypeField.Type.ToString()) 
                         }
                         
                         $ContentTypeWorkflowsElement = $ContentTypeElement.AppendChild($xmlDoc.CreateElement("Workflows"));
                         foreach($workflow in $ContentType.WorkFlowAssociations)
                         {
                             $ContentTypeWorkflowElement = $ContentTypeWorkflowsElement.AppendChild($xmlDoc.CreateElement("Workflow"));
                             $ContentTypeWorkflowElement.SetAttribute("DisplayName", $workflow.Name) 
                             $ContentTypeWorkflowElement.SetAttribute("InternalName", $workflow.InternalName) 
                             $ContentTypeWorkflowElement.SetAttribute("Id", $workflow.Id) 
                         }
                     }
                     
                     ### Content Types Ends
                 }
                 if($Config.Configuration.ListInfo.Workflows -eq "True" -or $Config.Configuration.ListInfo.FetchAll -eq "True")
                 {
                     ## Workflow Starts
                     
                     $WorkflowsElement = $ListElement.AppendChild($xmlDoc.CreateElement("Workflows"));
                     $WorkflowsElement.SetAttribute("Count",$List.WorkflowAssociations.Count);
                     
                     foreach($Workflow in $List.WorkflowAssociations)
                     {
                         $WorkflowElement = $WorkflowsElement.AppendChild($xmlDoc.CreateElement("Workflow"));
                         $WorkflowElement.SetAttribute("DisplayName", $Workflow.Name) 
                         $WorkflowElement.SetAttribute("InternalName", $Workflow.InternalName) 
                         $WorkflowElement.SetAttribute("Id", $Workflow.Id) 
                         
                         if($Workflow.RunningInstances -ge 1)
                         {
                             $WorkflowElement.SetAttribute("IsRunning", "True") 
                         }
                         else
                         {
                             $WorkflowElement.SetAttribute("IsRunning", "False") 
                         }
                         
                         if($Workflow.InstantiationUrl -like "*NintexWorkflow*")
                         {
                             $WorkflowElement.SetAttribute("IsNintex", "True") 
                         }
                         else
                         {
                             $WorkflowElement.SetAttribute("IsNintex", "False") 
                         }
                     }
                         
                     ## Workflow Ends
                 }
             }
         }
         $xmlDoc.Save($ListInfo_XML_Path)
     }  
      
     Write-Host "Completed GetListInfo Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetListInfo Method"
     $script:Progress = "8:Success"      
 }
 
 function IsInfoPathUsed($List)
 {
     $InfoPath_Used = "False"
     if($List.BaseTemplate -eq "XMLForm")
     {
         foreach($item in $List.Items)
         {
             $Extension = $item.Name
             $Extension = $Extension.Substring($Extension.IndexOf(".")+1)
             if($Extension -eq "xsn" -or $Extension -eq "xml")
             {
                 $InfoPath_Used = "True"
                 break
             }
         }
     }
     return $InfoPath_Used
 }
 
 ############ End of Method ########
 
 
 

The snippet is self-explanatory.

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation