How to Retrieve Basic SharePoint List Information – PowerShell

Sathish Nadarajan
 
Solution Architect
June 4, 2016
 
Rate this article
 
Views
5929

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

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment