Tuesday, November 3, 2015

SharePoint GetFieldValueAsHtml or GetFormattedValue with DateTime fields Show One Day Earlier

Question:

What am I doing incorrectly here?  I simply want to get the date value from an SPFieldDateTime, which I thought was working until I looked into it closer:

1) I input 10/01/2008 into the field
2) in the code, it consistently reads 09/30/08
(tested with mulitple dates, it always removes a day from the inputted value)

the kicker is, however, that the field contains the correct date!!!
see below, the object is the same throughout:

?field.GetFieldValue (listItem(field.Id))
"10/1/2008" {String}
String: "10/1/2008"
?field.GetFieldValueAsText (listItem(field.Id))
"9/30/2008"
?field.GetFieldValueForEdit (listItem(field.Id))
"9/30/2008"
?field.GetFieldValueAsHtml (listItem(field.Id))
"9/30/2008"
?listitem(field.Id).ToString
"10/1/2008 12:00:00 AM"

Answer:

Watch out when using GetFieldValueAsHtml or GetFormattedValue with SharePoint DateTime fields! They both expect the value to be in UTC! However, SPListItem[FieldName] returns its value in Local Time zone and marks it as Unspecified.

Basically, what you have to do prior to calling one of the two methods above is make sure your time is in UTC:

DateTime localTime = (DateTime)listItem[“SomeDateTime”];
DateTime universalTime = this.Web.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctlyConverted = listItem[“SomeDateTime”].GetFieldValueAsHtml(universalTime);

Thursday, October 29, 2015

Delete All Items SharePoint Powershell Script

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

write-host

# Enter your configuration here
$siteUrl = "http://mysharepointsite.example.com/"
$listName = "Name of my list"
$batchSize = 1000

write-host "Opening web at $siteUrl..."

$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
write-host "Web is: $($web.Title)"

$list = $web.Lists[$listName];
write-host "List is: $($list.Title)"

while ($list.ItemCount -gt 0)
{
  write-host "Item count: $($list.ItemCount)"

  $batch = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"
  $i = 0

  foreach ($item in $list.Items)
  {
    $i++
    write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline

    $batch += "<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">$($item.File.ServerRelativeUrl)</SetVar></Method>"

    if ($i -ge $batchSize) { break }
  }

  $batch += "</Batch>"

  write-host

  write-host "Sending batch..."

  # We execute it
  $result = $web.ProcessBatchData($batch)

  write-host "Emptying Recycle Bin..."

  # We remove items from recyclebin
  $web.RecycleBin.DeleteAll()

  write-host

  $list.Update()
}

write-host "Done."

Monday, October 19, 2015

Displaying search results to anonymous users in SharePoint 2013

Here’s a SharePoint 2013 situation I’ve run into: you have items in a list/library that cannot be viewed by anonymous users. You have some search related web part such as a content search or search result web part that is supposed to display these items. The problem I ran into is that I wanted information about these items to be displayed in the search result web part while ensuring that anonymous users cannot actually view the items. For example, you may have a list of publications that have a title, publication date and a body of content (see see image).
A SharePoint 2013 list or library with content
A SharePoint 2013 list or library with content
In such a case, I want everyone to be able to see the title and the publication date, however I do not want anonymous users to be able to see the body. The search result web part (via the use of a display template) will only show the title and publication date. If they want to view the content of the item, they have to click on the search result to open it up at which point they will be able to either view the item if they are logged in, or be prompted to log in if they are currently anonymous. Out of the box, if you have a search web part trying to display data to two different types of users (anonymous and authenticated users), with the scenario described above, authenticated users will see results while anonymous users will see no results (see images below)
Results will be shown for authenticated users
Results will be shown for authenticated users
Anonymous users will be given the standard nothing here matches your search message
Anonymous users will be given the standard nothing here matches your search message
Now I’ve seen a few ways that supposedly fix this,and my last search for this issue led me to Soeren L. Nielsen solution for this particular issue. Not to say that his solution doesn’t work, but in my case it seemed that it didn’t work for SharePoint 2013. I found that the key to get this to work lies in the PowerShell script and altering the permissions for anonymous users. The PowerShell script provided in Nielsen’s solution shown below uses the AnonymousPermMask property and ViewFormPages enum:

$web = get-spweb “http://yoursiteurl/subweb/subsubweb”
$list = $web.Lists[“ListName”]
$list.BreakRoleInheritance($true)
$list.AnonymousPermMask = $list.AnonymousPermMask -bor ([int][Microsoft.SharePoint.SPBasePermissions]::ViewFormPages) #binary or adding the permissions
$list.Update()

In the case of 2013, I found that this works with the following changes
$web = get-spweb “http://yoursiteurl/subweb/subsubweb”
$list = $web.Lists[“ListName”]
$list.BreakRoleInheritance($true)
$list.AnonymousPermMask64= $list.AnonymousPermMask64 -bor ([int][Microsoft.SharePoint.SPBasePermissions]::AnonymousSearchAccessList) #binary or adding the permissions
$list.Update()

The two key changes are in bold here. The first is with AnonymousPermMask which according to MSDN is obsolete and you need to use AnonymousPermMask64. The second and probably key change is with the SPBasePermission enum. Rather than using ViewFormPages, you need to use AnonymousSearchAccessList which is described as “Make content of a list or document library retrievable for anonymous users through SharePoint search. The list permissions in the site do not change.”
Hope this helps anyone who ran into a similar challenge!

P.S. – Don’t forget to re-crawl your site after running the PowerShell script…

Thursday, July 23, 2015

JavaScript Function: HTML Encode for Multiple Lines Of Text Sharepoint

Recently I have found the need to HTML Encode text in JavaScript, especially when using update list items and Single Line Of Text or Multiple Lines Of Text fields.
An easy way to do this is use the native functionality of the browser when it processes DIV’s.
Any text inside of a DIV automatically gets HTML Encoded.  So all we have to do is create a DIV on the fly, insert the text and return the output.
1 function HTMLEncode(str) { 2 var div = document.createElement('div'); 3 var text = document.createTextNode(str); 4 div.appendChild(text); 5 return div.innerHTML; 6 }

Thursday, May 28, 2015

SharePoint 2013 Create a Custom Page Layout

With the new Designer Manager in SharePoint 2013 it is now much easier to create a custom Page Layout. In this blog article I ‘m providing a step by step pictorial guidance on how to create a simple custom Page Layout.
My prerequisites: I have created a site collection based on the template ‘Publishing Portal’ and installed SharePoint Designer 2013.
image
What I need is a simple Page Layout with two columns with different widths. These columns should be created as Web Part Zones to enable editors to place Web Parts on the page.
First I open the site settings and start the new Design Manager:
image
Because I want to create a custom Page Layout I click on ‘Edit Page Layouts’ …
image
… and create a new Page Layout (based on a predefined master page and the ‘Article Page’ content type):
image
It should take just a few seconds until SharePoint has created the new Page Layout.
image
OK - now let’s switch over to SharePoint Designer 2013. To find the newly created Page Layout I click on ‘All Files’ in the left navigation. Now I open the folder _catalogs and click on master page. I have marked the files of the newly created Page Layout with a red rectangle.
image
The newly created Page Layout consists of two files. Don’t touch the aspx-file! To create a custom Page Layout you only need to edit the html-file. SharePoint will create and update the aspx-file based on the changes in the associated html-file.
Let’s open the html-file to see what SharePoint has created. I check out the html-file and open it in Advanced Mode:
Now this is what you should see:
image
For this demo I have chosen the Article Page as the template for my new Page Layout - but I don’t want to keep most of the controls. So I scroll down to the area of the html-file where the page controls are defined.
image
The page controls are surrounded by <div> tags - so they should be pretty easy to find. Now I locate this line of code:
<!--MS:<asp:ContentPlaceHolder ID="PlaceHolderMain" runat="server">-->
and delete everything beginning from (but excluding) the line above until (excluding) this line:
<!--ME:</asp:ContentPlaceHolder>-->
In other words: I delete every line of code between these two (ContentPlaceHolder) lines (but I keep these lines).
With this deletion I have removed almost every page control and the newly created custom Page Layout file should look like this (I marked the deleted area with an empty red box):
image
Let’s see how this empty Page Layout looks like in SharePoint 2013. I save and check-in the html-file in SharePoint Designer 2013 and switch back to SharePoint 2013. Have look on the fifth screenshot again - here I click on the name of my newly created Page Layout and SharePoint 2013 opens this page:
image
Looks like a pretty empty page! To better check the changes in my custom Page Layout I create another page that uses my newly created custom Page Layout.
This is what the newly created page based on my custom Page Layout now looks like:
image
This page is empty in edit mode too, because I just removed all page controls. Now it’s time to add two Web Part zones..
I switch back to SharePoint Designer 2013 and open the html-file in Advanced Mode again. In the gap where I deleted the page controls before I now add a simple html table like this:
image
Just a simple table with one row and two columns (in other words: two parallel table cells). The first column should have a width of 80% and the second one should have a width of 20%. Both columns should be top-aligned.
The next step is: I need to add two Web Part Zones: one in each table’s column. I save the changes in SharePoint Designer 2013 and switch back to SharePoint’s Design Manager.
I open this page …
image
… and click on Snippets:
Now the Snippets Designer is shown and I click on Web Part Zone:
image
The Snippets Designer creates the HTML code for a new Web Part Zone and I can copy the HTML code snippet to the clipboard. You can customize some of the parameters of the Web Part Zone (or any other snippet) before copying the snippet to the clipboard. To edit parameters click on the greyed parameter names on the right.
image
Now I jump back to SharePoint Designer, open the html-file again and paste the snippet to the first table column. After that I switch back to the Snippets Designer and create another (a new - that’s important) Web Part Zone snippet with a new ID (!) and copy the snippet’s code to the second table column.
My html-file looks like this now:
image
Did you notice the doubled Page Title in the 10th screenshot? Doesn’t look very nice - that’s why  I remove the placeholder with the ID PlaceHolderPageTitleInTitleArea. I don’t want the page title here - for my demo the name of the aspx-file is sufficient. I save the changes in SharePoint Designer 2013 and check-in the html-file again.
Let’s see how the newly created Page Layout looks like now. I switch back to SharePoint 2013 and open the site with the newly created Demo Page … and hit F5 to refresh the page:
image
There are now two Web Part Zones with a different width and both are top-aligned! Just to prove that this Page Layout is working as expected I add some Web Parts to the two zones and publish the page:
image
That’s how I wanted the custom Page Layout to look like. If you want to create a more detailed Page Layout just have a look at the snippets. There are several useful snippets you can use in your own custom Page Layout by simply getting the HTML code of the snippet and pasting it to the custom Page Layout.
image
Compared to former version of SharePoint creating custom Page Layouts has been simplified very much. The new Design Manager in SharePoint 2013  (together with the Snippets) makes it easier to create custom Page Layouts. Use this blog article as a short introduction and feel free to experiment with Page Content Types, snippets and custom Page Layouts on your own.

SharePoint 2013 Branding Step By Step

SharePoint 2013 includes several new features and capabilities to support branding aspects that includes better support for HTML5, Design Manager, Lots of Inbuilt design templates, Composed Looks, support for Responsive web design, HTML driven MasterPage.
This post is regarding step by step Sharepoint 2013 branding to have look and feel of internet facing site. This is also example of branding our SharePoint online live site.

Testing it out

I don’t have enough time to create my own HTML design so I went on the net and looked for something that would do. I downloaded this free theme: http://demo.templateworld.com/zero/download/dusky.zip
First I started by uploading the files in a folder called Dusky under https://niaulin-public.sharepoint.com/_catalogs/masterpage/ which is the url Design Manager gave me to upload my design files.
I recommend you map yourself a drive to that location because you will probably be going back and forth making adjustments and adding files.
Uploaded the files using the same structure provided in the .zip to make sure paths continued to work.

To get started make sure user logged in has proper rights atleast design rights in order to follow below steps.
Step 1: To create a master page go and click Site Settings icon
sharepoint-branding-1.png
Step 2: Select Site Settings
sharepoint-branding-2.png
Step 3: In the Site Settings --> Look and Feel --> select Design Manager.
sharepoint-branding-3.png
Step 4: In the Design Manager, -->Select Upload Design Files link.
sharepoint-branding-4.png
Step 5: Now open the link shown below to add content, html files, css, scripts for your new Master Page.
sharepoint-branding-5.png
Step 6: Add all the content required to create new master page such as html page, css styles, scripts. Click on Files in the ribbon and start uploading files in a newly created folder.
sharepoint-branding-6.png
Step 7: Create a new folder for example SPEmpower.
sharepoint-branding-7.png
Step 8: After creating the main folder, add contents like css, js,images ans html page inside this folder.
sharepoint-branding-8.png
Step 9: Create folders for css, javascript and images and also add html page.
sharepoint-branding-10.png
Step 10: Now to add document in this folder select Upload Document from File menu and a window will open, browse your document and then click on ok button.
sharepoint-branding-17.png
Step 11: Now another window will open, click on Save button to add that document inside the folder.
sharepoint-branding-18.png
Step 12: Same way you can add more css in this and to add Javascript and images do the same process, First create the folder and then add documents you want to add. After that add the html page. Here browse your html page and then click on Ok button.
sharepoint-branding-11.png
Step 13: After clicking on Ok button a window will open click on save to add this html page to your folder.
sharepoint-branding-12.png
Step 14: After adding all the content inside folder, below is organized look of all the files of our new master page
sharepoint-branding-19.png
Step 15: Next, Go to Design Manager select Edit Master Pages and then select Convert an HTML file to a SharePoint master page to convert your html page into the master page.
sharepoint-branding-20.png
Step 16: Select your Master Page folder.
sharepoint-branding-21.png
Step 17: Select the html page and click on insert to convert the html page into master page.
sharepoint-branding-22.png
Step 18: Now to see your converted page, Go to Design Manager then select Upload Design Files and open the below link
sharepoint-branding-23.png
Step 19: Now select the folder SPEmpower.
sharepoint-branding-24.png
Step 20: We can see the .html file as well as the converted .master file as well.
sharepoint-branding-25.png
Step 21: After doing all this. It's time to publish the content of your project. We publish every files within the folder such as css, images, .html page. To publish go to Design Manager and then click on Publish and Apply Design and then select Go to the master page Gallery.
sharepoint-branding-26.png
Step 22:Go to the project folder "SPEmpower".
sharepoint-branding-27.png
Step 23: Go to File menu and then select any content from the bottom by clicking on check box and then publish button will be enabled, click on publish button to publish the file.
sharepoint-branding-29.png
Step 24: When you click on publish a new window will open, If you want to add any comment with that add and then click on ok to publish that document.
sharepoint-branding-30.png
Step 25: Your master page is published now. By default SharePoint site uses Seattle.master page, to apply your master page to your SharePoint site. Go to SIte Settings and then select Master Page from Look and Feel
 sharepoint-branding-33.png
Step 26: Select our own master page to apply that to our site.
sharepoint-branding-34.png
Step 27: Select Site Master Page and from It's drop down list select your master page.
sharepoint-branding-35.png
Step 28: Select your master page as a System Master Page if you want apply custom master in system pages like Site Settings page.
sharepoint-branding-36.png
Step 29: Click on Ok to apply your master page to your site.
sharepoint-branding-37.png
Step 30: After applying the master page your site will look like this.
sharepoint-branding-38.png
Step 31: Here the main content of your site will come at the bottom of the page after footer. so you have to cut that main content placeholder from bottom and will have to add it in the proper place. you can do it by using SharePoint Designer.
sharepoint-branding-39.png
Step 32: Now first open your Sharepoint Designer and open your site from that.
sharepoint-branding-40.png
Step 33: Now from Navigation select Master Pages.
sharepoint-branding-41.png
Step 34: Now After doing this select your folder named SPEmpower.
sharepoint-branding-42.png
Step 35: Now select your html file Sharepointempower.html.
sharepoint-branding-43.png
Step 36: Now from Customization select Edit File to add any content in that file. you will not require to open Sharepointempower.master page whatever you will add in the Sharepointempower.html page,It will automatically update .master page.
sharepoint-branding-44.png
Step 37: Go through the code and will go to the bottom, you can see that after footer the MainContentPlaceHolder code is there. Just cut that selected div tag and paste it in the code where Main-Content section is given.
sharepoint-branding-45.png
Step 38: Paste that code in the selected section area called Main-Content.
sharepoint-branding-46.png
Step 39: Once It is done. Your Master page's MainContentPlaceHolder will be there in the center of the page.
sharepoint-branding-47.png