PowerShell Upload File to Style Library & Sub-Path Location

function UploadFile2StyleLibraryLocation($WebUrl, [String] $SourceFilePath, [String] $StyleLibrarySubPath) { # Open web $web = Get-SPWeb $WebUrl $file = Get-Item $SourceFilePath write-host "Started Uploading File..." $file.FullName # Open file $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() # Open Style Library $folder = $web.getfolder("Style Library") # Check whether the file is already exists? $File2Replace = $web.GetFile($folder.Url + $StyleLibrarySubPath + $file.Name) if($File2Replace.Exists -eq $true){ $File2Replace.CheckOut() } # Add the file $spFile = $folder.Files.Add($folder.Url + $StyleLibrarySubPath + $file.Name, [System.IO.Stream]$fileStream, $true) # Check in $spFile.CheckIn("Checkin by deploy script 1.7.0") # Finally publish the file $spFile.Publish("Published by deploy script 1.7.0") $MessageFilePath = $WebUrl + "/" + $folder.Url + $StyleLibrarySubPath + $file.Name $MessageFileName = $file.Name write-host $MessageFilePath write-host "Successfully uploaded file $MessageFileName" $fileStream.Close(); $web.Dispose() }



UploadFile2StyleLibraryLocation -WebUrl "http://win12sp13"` -SourceFilePath "C:\mywork\pwcs-customer\Phase2Development-V1_7_0\Readify.Pwcs.Deployment\V1.7.0\_package\Deploy\Content\promotion.js"` -StyleLibrarySubPath "/Readify/ClientTemplates/"

Url/Uri combine

Isn’t all of us wonder whether there is a good utility method available out of the box to combine uri together, I mean something like Path.Combine().


System.IO.Path.Combine() combines two path strings and manages the trailing separator character (backslash (“\”)).

Using Path.Combine() you will get an output which mixes backslashes with forward slashes:
string output = System.IO.Path.Combine(“http://www.domainname.com/dropbox”, “egomo.txt”);
// output = http://www.domainname.com/dropbox\\egomo.txt

Though you could try using – VirtualPathUtility.Combine() it has its drawback as the virtual path must exist and need access to it, otherwise an exception will be thrown.

Another alternative to this is using Uri class, this also have an interesting issue:

Uri baseUri = new Uri(“/BaseUriPath”);

Uri relativeUri = new Uri(“/relativepath”)

Uri finalPath = new Uri(baseUri, relativeUri)

The above will give you an output “/relativePath”

so, I decided to write my own utility method which is smart and serves my need:

        /// <summary>
        /// Utitlity method to combine a baseUrl with a relateive url.
        /// </summary>
        /// <param name="baseUrl">Base url.</param>
        /// <param name="relativeUrl">Url relative to the base url.</param>
        /// <returns>Combined url.</returns>
        private static string UrlCombine(string baseUrl, string relativeUrl)
            if (baseUrl.Length == 0)
                return relativeUrl;
            return relativeUrl.Length == 0
                       ? baseUrl
                       : string.Format(CultureInfo.InvariantCulture, "{0}/{1}", baseUrl.TrimEnd(new[] {'/', '\\'}),
                                       relativeUrl.TrimStart(new[] {'/', '\\'}));