Skip to content

Instantly share code, notes, and snippets.

@EricRohlfs
Created July 16, 2015 20:16
Show Gist options
  • Save EricRohlfs/641d3d24d92aea258963 to your computer and use it in GitHub Desktop.
Save EricRohlfs/641d3d24d92aea258963 to your computer and use it in GitHub Desktop.
# ---------- Main Vars to Change-----------------
$contentPath = 'C:\MyApp\content'
# find the file at example : C:\Users\me\AppData\Local\Temp on my computer
$OutPath = "$env:temp\SharePointProjectItem.spdata.fragment.xml"
# --------- End Main Vars to Change------------
# Code Summary
# Creates an xml fragment to copy and paste into the SharepointProjectItem.spdata file.
# This can save days of work on large projects
# The SAX method for writing xml is being used. It is very declaritive.
# Code
# open the current file and copy the code here
$sharePointDeployRoot = '..\..\MyApp\MyApp\'
Function WriteSpXmlLine($sourcePath, $targetPath, $spType)
{
$xmlWriter.WriteStartElement('ProjectItemFile')
$XmlWriter.WriteAttributeString('Source', $sourcePath)
$XmlWriter.WriteAttributeString('Target', $targetPath)
$XmlWriter.WriteAttributeString('Type', $spType)
$xmlWriter.WriteEndElement()
}
# get an XMLTextWriter to create the XML
$XmlWriter = New-Object System.XMl.XmlTextWriter($outPath,$Null)
# choose a pretty formatting:
$XmlWriter.Formatting = 'Indented'
$XmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
# loop through all the files and folders in $contentPath and make an entry where we do not have a folder.
Get-ChildItem $contentPath -recurse |foreach($_) {
if(! $_.PSIsContainer){
# get the folder name from full name
$targetFolder = Split-Path $_.FullName -parent
# remove the $contentPath from the name
$targetFolder = $targetFolder.replace( $contentPath + '\', '')
$source = (Join-Path $sharePointDeployRoot $_.name)
Write-Host $source
# call our function to write a line of xml
WriteSpXmlLine $source $targetFolder 'ElementFile'
}
}
$XmlWriter.Flush()
$XmlWriter.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment