Wednesday, April 29, 2015

QuickTip: Display multiple values based on selection in InfoPath

I have a form with a selection box where the user selects a Role, and based on that Role I set a multiselect field with the predefined values, in this case Applications. What I wanted to do, was to have the checkboxes selected by default OR not display the checkboxes at all. Because this should be a list of all applications that are included when you select a specific role, so the user should not be able to check or uncheck any boxes. I select a role in the left drop down box, and all the values are shown to the right in a Multiselect box:

It does not seem possible to have all values selected in the Multiselect field, only the top value can be selected and that is not what I want. It's either ALL values or NO checkboxes. Well simple enough I chose the "ListBox" instead, that rolls up all values without checkboxes. The only downside is that you can select one or more values but I have not published the values in the fields anywhere and I am not saving them so it is fine. So the left drop down box selects a role and the listbox shows all values separated by a new line:

Display comments and likes in CQWP

This is an update from my former blog post about showing likes and comments in a news web part. And I also get questions about how I have done this, and people saying that they never managed to do the same. So even if I updated the former blog post with this solution, it may not have been clear enough how I did it.

First of all, I changed from using the CSWP (Content Search Web Part) to use the CQWP (Content Query Web Part) as I never got the Comments to work without always running a Full crawl (and we don't want that!).

Then I create the news on a separate Blog site. The news will then be rolled up on the intranet front page in the following format:

As shown in the picture above, the news item is rolled up displaying Published Date, Title, Likes and Comments. Sometimes I also add the Category, but that part is easy and if you can add one you can add another of these fields, so I will mostly focus on how I did the Like and Comments part.

To modify the CQWP you must first add your own xsl files.
NOTE: do NOT modify the existing files - they might get overwritten in an update OR you might write something totally wrong and nothing will work on your site. You always create a new folder called "Custom" (or whatever you want), then you COPY existing files and paste them into your folder and there you can modify them without taking a risk.

Easiest is to open your site in SharePoint Designer, browse to the folder Style Library > XSL Style sheets > Create your own folder "Custom". Then copy the file called "ItemStyle.xsl" and paste into this new folder.

Now go to the site where you first add the CQWP and then export that same web part. You export a web part by clicking the arrow on the web part and then select "Export":

Open the file, it might be called "News.webpart" or whatever you named the web part, and find the row with the property name "ItemXslLink" and change the path to the new that was created above:

Tag: property name="ItemXslLink" type="string"
New value: /Style Library/XSL Style Sheets/Custom/ItemStyle.xsl

It should look like this in the webpart file now:

Then add the fields that should be displayed in the web part, they need to be added in a special way and you must find out their GUID. Find the property tag called "DataMappingViewFields" and the fields must be added in this syntax: {GUID}, Type and then separated with ;

To find the GUID of the fields that should be displayed, go to the list where the columns are and hover each column or click on it and look in the address field. The URL will contain the GUID. Be sure to remove the ASCII for % and - since they are in the URL.
I want to get the field where Comments are, (which is a Lookup Field) so I go to my news blog and open the Posts list. Go to the List settings and scroll down to find the Column field. Click on it and copy the URL and then paste that into Notepad. You will get something like this:

Then in Notepad, copy only the GUID from the URL:


And then replace all ascii characters with the normal character like

%7B which is {
%7D which is }
%2D which is -
and you will get


Do this for all fields that you want to display in the web part, like Comments, Likes, Category etc

Before you upload the web part file, go back to SharePoint Designer and open the custom xsl file. Add a new xsl:template in the file like this (it is an image):

The entire code in this is here, but the < tags are exchanged to [] so that the code can be displayed here on the page (just replace the [] with the regular < tags):

[?xml version="1.0" encoding="utf-8" ?]

  exclude-result-prefixes="x d xsl msxsl cmswrt"
  xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt"]
[xsl:param name="ItemsHaveStreams"]
    [xsl:value-of select="'False'" /]
[xsl:variable name="OnClickTargetAttribute" select="string('"_blank"')" /]
[xsl:variable name="ImageWidth" /]
[xsl:variable name="ImageHeight" /]

[xsl:template name="BigImage" match="Row[@Style='BigImage']" mode="itemstyle"]
[xsl:variable name="SafeImageUrl"]
            [xsl:call-template name="OuterTemplate.GetSafeStaticUrl"]
                [xsl:with-param name="UrlColumnName" select="'ImageUrl'"/]
[xsl:variable name="SafeLinkUrl"]
            [xsl:call-template name="OuterTemplate.GetSafeLink"]
                [xsl:with-param name="UrlColumnName" select="'LinkUrl'"/]
[xsl:variable name="DisplayTitle"]
            [xsl:call-template name="OuterTemplate.GetTitle"]
                [xsl:with-param name="Title" select="@Title"/]
                [xsl:with-param name="UrlColumnName" select="'LinkUrl'"/]
[div id="Item_LargePicture" style="padding-bottom:10px"]

        [div class="cbs-largePictureContainer" id="_#= containerId =#_" data-displaytemplate="ItemLargePicture"]
            [div class="cbs-largePictureImageContainer" id="_#= pictureContainerId =#_"]
                [xsl:if test="string-length($SafeImageUrl) = 0"]
[a href="{$SafeLinkUrl}"]
[img src="/_layouts/15/images/custom/emptynews.png" class="cbs-pictureImgLink" /]
[xsl:if test="string-length($SafeImageUrl) != 0"]
[a href="{$SafeLinkUrl}" class="cbs-pictureImgLink" ]
[xsl:if test="$ItemsHaveStreams = 'True'"]
[xsl:attribute name="onclick"]
                          [xsl:value-of select="@OnClickForWebRendering"/]
[xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'"]
[xsl:attribute name="onclick"]
                        [xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/]
[img src="{$SafeImageUrl}?RenditionID=5" title="{@ImageUrlAltText}" class="cbs-pictureImgLink" id=""/]

            [div class="cbs-largePictureDataOverlay"  id="_#= dataContainerOverlayId =#_"][/div]
            [div class="cbs-largePictureDataContainer" id="_#= dataContainerId =#_"]
                [a class="cbs-largePictureLine1Link"]

[h2 class="cbs-largePictureLine1"]

[a href="{$SafeLinkUrl}" title="{@LinkToolTip}" class="cbs-largePictureLine1Link"]
[xsl:if test="$ItemsHaveStreams = 'True'"]
[xsl:attribute name="onclick"]
                  [xsl:value-of select="@OnClickForWebRendering"/]
[xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'"]
[xsl:attribute name="onclick"]
                      [xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/]
[xsl:value-of select="$DisplayTitle"/]

                [span title="_#= $htmlEncode(line4) =#_" id="_#= line4Id =#_" ]
                [xsl:value-of select="ddwrt:FormatDateTime(string(@PublishedDate) ,1053 ,'dd MMMM')"/][br /]
                [xsl:value-of select="@PostCategory"/][/span]
                [span title="_#= $htmlEncode(line5) =#_" id="_#= line5Id =#_"]
                [span style="padding-left:180px;"][xsl:if test="string-length(@LikesCount) = 0"] 0[/xsl:if]
[xsl:value-of select="@LikesCount" /][span style="padding-left:3px;vertical-align:text-bottom"][img src="/_layouts/15/images/custom/like.png" border="0" /][/span][/span][span style="padding-left:10px;"][xsl:value-of select="@NumComments" /][span style="padding-left:3px;vertical-align:text-bottom"][img src="/_layouts/15/images/custom/comment.png" border="0" /][/span]



Okey so now we have a style for the news items. But there still are some things more to do.

Enable Image Rendition and Blob Cache to make the images render in the correct size, like thumbnails.

First upload this new web part into the sites WebParts gallery (its better to do that than to just upload it straight into the page, and you can also give it a proper name and put it in its own folder so its visible in the gallery when you add a new web part)

Then go to the start page and add this new web part. You have to modify it to point out the news blog location, and to select the correct display for it. And to make other settings that you need, like how many items that should be shown, what sorting order you want, if you want to add any filters etc.

In the web part settings, also go down to the section where the fields are mapped. Then add the names of the columns that you want to display, like PublishedDate, Category, Likes etc and you will find that there are new placeholders for these as we added those in the webpart file:

Yes it is in Swedish, but if you are familiar with CQWP (which I really hope you are, since this is quite complex to setup) then you should recognize the fields I am talking about. So, type in the name of the fields you want to display and DONE. But what? No, you cannot find a field for Comments? How surprising ;) This is where we need to go to Central Administration on the server and a new Mapping property in the Search app.

Go to Central Admin, open the Search service app, click on the link "Search schema" in the left menu and there add a "New managed property":

Call it what you want, like "NumberOfComments". Then set it to be Searchable, Retrievable, and then scroll down to the mapping. Add a new mapping and find the field called "ows_NumComments" and then Save this. You MUST run a full crawl for this to apply.

Okey, that was it. Simple right?? :) :)

Tuesday, April 28, 2015

QuickTip: Enable Anonymous access in SharePoint 2013

When you want to publish a SharePoint site as a public website, you must enable anonymous access - otherwise the user will be prompted with a login. Note: This is for on premises servers and not the cloud/Office365.

Open Central Administration on your server. Go to the Application Management section and then click the “Manage web applications” link.

Select the web application where you have your public web site collection and then click the “Authentication providers” button available in the Ribbon. It will most likely show a "Default" link:

Click on the “Default” and then check the “Enable anonymous access” option and then Save.

Now open the site collection that will be used as public website and go to the Site Settings. Click on "People and Groups". In the ribbon there is a button for “Anonymous Access” that allows you to configure how anonymous users can access to the site.

Click the “Anonymous Access” button in the Ribbon and find three options, select the one that is suitable for your site. In this case I would check the "Entire Web Site" to give the users full anonymous access to the site.

Now when you go back to the “People and Groups” page, there should be a new group called “Anonymous users” available in the list and it shows what permission they have on the site.

To test the anonymous web site, make sure you are signed out of the browser you are testing in, and see if you can access the site withouth having to sign in.