1

Here’s what I am trying to do. There is an XML file in the “forms” folder of each our SharePoint libraries called “client_LocationBasedDefaults.html” The file contains the column defaults that can be set in Library settings. The the XML looks like this

<MetadataDefaults> <a href="/RestoreTest/App"> <DefaultValue FieldName="FunctionalClass">OPERATIONS</DefaultValue> <DefaultValue FieldName="BusinessUnit">Applications</DefaultValue> </a> <a href="/RestoreTest/App/Advance_Retail"> <DefaultValue FieldName="FileNumber">REST10001-000002</DefaultValue> <DefaultValue FieldName="FileLocationName">Restore Test==Restore Apps==Advance Retail</DefaultValue> <DefaultValue FieldName="ITSystem">Point of Sale</DefaultValue> </a> </MetadataDefaults> 

Here is a snippet of PowerShell which I have been experimenting with for a couple of days. I am able to pull out the href information no problem as well as the fieldnames but not the actual defaultvalues for example 'Point of Sale' . I think my knowledge of XML is probably wanting here. Anyway if someone could point me in the right direction I would be really grateful as I have pretty much exhausted my own limited knowledge and examples I have seen on the net don’t seem applicable.

$default = $document.OpenBinary() $encode = New-Object System.Text.ASCIIEncoding [xml]$defaultXML = ($encode.GetString($default)) $content = $defaultXML.metadatadefaults foreach($w in $content.a) { if($w.href){ Write-Host $w.href } } 
7
  • Could you provide the XML-file and explain which default value you want to read from PowerShell? From the looks of it, you only need to append the XML-tag in $content.[xml-tag] and took for the XML-attribute in you if-statement. But the value can also be stored as a tag-value and not an attribute. So please edit your question, and add the XML-string you want to read from PowerShell.CommentedNov 9, 2013 at 9:54
  • 1
    Ooops how could I forget the XML. I can't figure out how to make it readable on the page.
    – tonym
    CommentedNov 9, 2013 at 19:19
  • 1
    jref? Is that right?CommentedNov 9, 2013 at 19:28
  • 1
    Good pickup james. Fixed it.
    – tonym
    CommentedNov 9, 2013 at 20:46
  • 1
    Strange that a topic related to a SharePoint XML which is central to its column defaulting functionality is off topic. I would imagine that being able to read that XML within the content of SharePoint would be useful to others especially those like myself trying to figure out why defaults in the XML are not corresponding with defaults actually applied to documents. Essentially that is the whole point of the exercise.
    – tonym
    CommentedNov 11, 2013 at 22:19

2 Answers 2

0

If you want to list all values of all DefaultValue tags, you can do something like this:

foreach($w in $content.DefaultValue) { Write-Host $w.InnerXML } 

Saving the array to a file, do this:

$output = @() foreach($w in $content.DefaultValue) { Write-Host $w.InnerXML $output += $w.InnerXML } $output > c:\temp\file.txt 

and you will have a value on each row in the text file.

Reference: Getting XML values in PowerShell

6
  • Yes that does list all values but I was hoping there would be easy way of capturing the values for each href in some kind of report. Something like this:
    – tonym
    CommentedNov 10, 2013 at 9:34
  • @tonym To a text file or to a html page?CommentedNov 10, 2013 at 9:45
  • $XMLContent = [XML](Get-Content -path "D:\SharePoint_2010\DefaultXML.xml") $XMLContent.SelectNodes('MetadataDefaults/a') href DefaultValue ---- ------------ /RestoreTest/App {DefaultValue, DefaultValue} /RestoreTest/App/Advance_Retail {DefaultValue, DefaultValue, Default... But with the default values for each field showing. This would have been perfect if it actually displayed the values along with the corresponding fieldname.
    – tonym
    CommentedNov 10, 2013 at 9:52
  • Text file would be good.
    – tonym
    CommentedNov 10, 2013 at 9:54
  • @tonym I've edited my answer to include writing an array to a text file.CommentedNov 10, 2013 at 12:57
0

I would recommend querying based on XPath. The following should get you what you want:

[XML]$XMLContent = Get-Content -path "D:\SharePoint_2010\DefaultXML.xml" $XMLContent.SelectNodes('//a//DefaultValue[@FieldName]') 

A breakdown of the XPath: Any 'a' (//a) node that has a 'DefaultValue' (//DefaultValue) child node which has an attribute of 'FieldName' ([@FieldName]).

That statement outputs:

FieldName #text --------- ----- FunctionalClass OPERATIONS BusinessUnit Applications FileNumber REST10001-000002 FileLocationName Restore Test==Restore Apps==Advance Retail ITSystem Point of Sale 

A couple of points to note when working with XML. Your XPath queries ARE case-sensitive. I've burned time in the past having a query fail because it was 'name' instead of 'Name'. Always double check if you think it should be working. Second, the text such as 'Point of Sale' can be accessed by .'#text'. The quotes are necessary otherwise PowerShell will throw an error. Here is an example use:

$content.SelectNodes('//a//DefaultValue[@FieldName]') | ` %{ Write-Host ("Field: {0}`t`tDefaultValue: {1}" -f $_.FieldName,$_.'#text')} 
2
  • This is pretty much what I was after except I want to work href into the equation as a report without it would be meaningless. I can see where I had been going wrong. I had been putting single slashes into the selectnodes. I would have stumbled on this had I got the syntax right. Is there some way I can add href into the report?
    – tonym
    CommentedNov 10, 2013 at 20:48
  • Something like this should get the job done: $content.SelectNodes('//a[@href]') | %{Write-Host $_.href; $_.DefaultValue | %{Write-Host ("Field: {0} DefaultValue: {1}" -f $_.FieldName,$_.'#text')}} You'll have to tweak to get the output just right.
    – beavel
    CommentedNov 11, 2013 at 0:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.