Wednesday, 28 October 2015

SharePoint: How do I get the location of the MySite url using PowerShell?


$mySiteUrl = ""
$sc = Get-SPServiceContext($mySiteUrl)
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($sc)
Write-Host $upm.MySiteHostUrl

NOTE: If you encounter a 'Permission Denied' error, make sure that your user has 'Full Control' permissions on the User Profile Service.

Monday, 26 October 2015

SharePoint: How do I upload a file structure to a document library?

My current project uses a custom 'style library' document library. The deployment process needed to replicate the files and file structure from the disk in the target library.

I used CSOM and Powershell to solve the problem.

I created a 'Common' dll that contains the methods I needed:
1. CreateFolder (to create a folder structure)
2. UploadFile (to load the file).

    public static class FileHelper
        public static Folder CreateFolder(Web web, string listTitle, string fullFolderPath)
            if (string.IsNullOrEmpty(fullFolderPath))
                throw new ArgumentNullException("fullFolderPath");
            var list = web.Lists.GetByTitle(listTitle);
            return CreateFolderInternal(web, list.RootFolder, fullFolderPath);

        private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderPath)
            var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string folderUrl = folderUrls[0];
            var curFolder = parentFolder.Folders.Add(folderUrl);

            if (folderUrls.Length > 1)
                var folderPath = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
                return CreateFolderInternal(web, curFolder, folderPath);
            return curFolder;

        public static void UploadFile(string siteUrl, string sourceFileFullLocation, string targetListTitle, string targetLocation)
            UploadFile(siteUrl, sourceFileFullLocation, targetListTitle, targetLocation, new Dictionary<string, string>());

        public static void UploadFile(string siteUrl, string sourceFileFullLocation, string targetListTitle, string targetLocation, Dictionary<string, string> items)
            using (var context = new ClientContext(siteUrl))

                var web = context.Web;

                var newFile = new FileCreationInformation()
                    Overwrite = true,
                    Content = System.IO.File.ReadAllBytes(sourceFileFullLocation),
                    //Url = targetLocation,
                    Url = Path.Combine(targetLocation, Path.GetFileName(sourceFileFullLocation))

                var targetFolder = context.Web.GetFolderByServerRelativeUrl(targetLocation);

                Microsoft.SharePoint.Client.File uploadFile = targetFolder.Files.Add(newFile);

                if (items != null)
                    foreach (KeyValuePair<string, string> item in items)
                        uploadFile.ListItemAllFields[item.Key] = item.Value;



Once the code was compiled into a dll, I instantiated them in my powershell code:

Function LoadDll($location)
$bytesCommon = [System.IO.File]::ReadAllBytes("$location")
$loadResultCommon = [System.Reflection.Assembly]::Load($bytesCommon)

The last part is to recursively loop through the folder structure and create the items in the target location

Function UploadFiles($siteUrl, $sourceFolder, $siteRelativeUrl)
$listName = "MyStyleLibraryList"
CreateLibrary $siteUrl $listName

Get-ChildItem $sourceFolder -Filter *.* -Recurse | 
$name = $_.Name
$folder = $_.FullName.Replace($sourceFolder, "").Replace("\", "/")
$folder = $folder.Substring(1, $folder.Length - 1)
if ($_.PSIsContainer -eq $true)
Write-Host "Creating folder $folder"
CreateFolder $siteUrl $listName $folder
$folderWithFileName = $_.FullName.Replace("$sourceFolder", "")
$folder = $folder.Replace($_.Name, "")
UploadFile $siteUrl  $_.FullName $listName "$listName/$folder" $folderWithFileName

Function CreateFolder($siteUrl, $listName, $folderName)
[MyHelperSolution.FileHelper]::CreateFolder($siteUrl, $listName, $folderName)

Function UploadFile($siteUrl, $sourceFolder, $listName, $siteRelativeUrl, $folderWithFile)
[MyHelperSoltuion.FileHelper]::UploadFile($siteUrl, $sourceFolder, $listName, $siteRelativeUrl)

Sunday, 4 October 2015

JQuery DatePicker: How do I add a watermark?

A watermark is always a great way to provider a meaningful hint to a user. Using the JQuery DatePicker control, I wanted to tell the user the expected format of the date.

 I added a new class called 'watermarkrequired' to the required html elements to help me identify them.

I looked at several option:
1. Using the blur and focus events.

$('.watermarkrequired').blur(function () {
if ($(this).val().length == 0)
}).focus(function () {
if ($(this).val() == watermark)

2. Using the 'AppendText' option of the DatePicker

$( ".watermarkrequired" ).datepicker( "option", "appendText", "(yyyy-mm-dd)" );

Finally, I settled on the placeholder attribute

$( ".watermarkrequired" ).attr("placeholder", "dd/mm/yyyy");