Linked Data Source/Lists

Display data from multiple sources in a single Data View this article also applies in SP2010

Display item counts in a Data View Web Part talks about having two lists and links them. Then displays a count of the sub list items.

 

Advertisements

Use noteboard to display comments for articles

To display a comment section on any publishing page, you can simply add a noteboard web part on the page or follow this article to add a social comment control on the page layout. In the article, author also talked about how to add the rating functionality.

 

Amol Meshe summarised a few points about the noteboard web part here.

Highlights:

  1. In order to use noteboard, User Profile service application needs to be associated with your web application.
  2. Number of replies to display can be configured via Note Board web part properties.
  3. Notes goes in the same DB and mapped against the page url.

 

Found this article that explains how to display comments count using DVWP and SOAP services.

 

These links talks about how to use code to get comment counts

http://sharepoint.stackexchange.com/questions/20389/number-of-comments-with-socialcommentmanager-getcomments

http://social.technet.microsoft.com/Forums/lv/sharepoint2010programming/thread/89be1264-b7a9-47bc-a6ac-86da7658580f

 

Create a custom HTTP404 rediect page in SharePoint 2010

By default, SharePoint takes you to its own 404 NOT FOUND page if you enter an Url which does not exist in the site.

My client has requested to display a 404 Not Found page with the custome master page and page layout style. After a few searches, some articles are found. Chaks’ and Ritu’s ones were particular helpful.

Default SharePoint 404 error pages are stored at this location:

14\TEMPLATE\LAYOUTS\1033\ERRORV4.html
14\TEMPLATE\LAYOUTS\1033\sps404.html

1. Create a error page on the site that has the publishing page layout and custom master page. I created a page called customError.aspx under the root site collection under the Pages folder.

2. Make a copy of sps404.html and name it custom404.html. Place it in the same location as sps404.html

3. Open custom404.html. Change the URL inside STSNavigate to your customError.aspx page url

customError

4. The next step is to set custom404.html as the 404 redirection page for your specific web application. we are accomplishing this by creating a custom404 feature and as the feature activates, it sets the FileNotFoundPage to the custom404.html. custom404.html then redirects the page to customError.aspx page

Open Visual Studio, create a new project, choose Empty SharePoint Project and change to .Net Framework 3.5. Name the project “PageNotFound”

Choose Deploy as a farm solution

Right click on Features, Add Feature, change the Scope to Site.

Right click on Feature1.feature, Add Event Receiver.

Open the EventReceiver.cs file, add this line above the namespace.

using Microsoft.SharePoint.Administration;

Paste the following code inside public class.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
      if (properties != null)
      {
        SPSite currentSite = (SPSite)properties.Feature.Parent;
        SPWebApplication webApplication = currentSite.WebApplication;
         webApplication.FileNotFoundPage = "custom404.html";
         webApplication.Update();
      }
}

Right click on the project and Deploy.

Now if you go to a incorrect page, you will be redirected to your custom error page.

Reference:
http://blog.mastykarz.nl/sharepoint-2010-page-not-found-404/
http://chakkaradeep.com/index.php/customizing-the-404-redirection-in-sharepoint/
http://ritutechworld.blogspot.co.nz/2010/06/custom-error-page-in-sharepoint-2010.html
http://microsoftechies.wordpress.com/2012/10/08/error-occurred-in-deployment-step-activate-features-unable-to-cast-object-of-type-microsoft-sharepoint-spweb-to-type-microsoft-sharepoint-spsite/

JavaScript – Open links on page in new window/tab in SharePoint 2010

There are two types links on page. One is that links added in the content as html. The other type is that links in a document library and displayed on page via DVWP.

Here is how to open the first type of links in new window:

1. Attach a javascript file in the master page or just add the javacript in the header section

2. Here is the javascript:

//add an entry to the _spBodyOnLoadFunctionNames array 
//so that our function will run on the pageLoad event 
_spBodyOnLoadFunctionNames.push("rewriteLinks");
function rewriteLinks() {  
//create an array to store all the anchor elements in the page   
var anchors = document.getElementsByTagName("a");   
//loop through the array   
for (var x=0; x<anchors.length; x++) {    
//set the target attribute to be blank to open in new window    
anchors[x].setAttribute('target', '_blank');   
} 
}

3. In IE, open Internet Options. General tab -> Tabs -> Settings ->When a pop-up is encountered -> Always open pop-ups in a new tab

Here is for the second type:

1. Attach a javascript file in the master page or just add the javacript in the header section

2. Here is the javascript:

$(document).ready(
  function ()
  {
    // has to be on an interval for grouped doc libraries
    // where the actual links are loaded only once a group
    // is expanded
    setInterval(
      function ()
      {
        $("a[onclick*='return DispEx'][target!='_blank']")
          .attr("target", "_blank")
          .removeAttr("onclick");

        // document type icons
        $("td.ms-vb-icon>img[onclick]:not([documentUrl])")
          .click(function (e)
          {
            window.open($(this).attr("documentUrl"), "_blank");
            e.stopPropagation();
            e.preventDefault();
            return false;
          })
          .each(function ()
          {
            $(this).attr(
            "documentUrl",
            $.trim(String($(this).attr("onclick"))
              .split("=")[1]
              .replace(/["'{}]/g, "")
              .split(";")[0])
            );
            this.onclick = null;
          });
      },
      500
    );
  }
);

Reference:
http://www.benramey.com/2011/04/25/opening-all-sharepoint-2010-documents-in-a-new-window/
http://www.sharepointkings.com/2008/07/open-documents-in-new-window.html

Limit access for users to add and edit list items

On my publishing page, I have a button to allow users to add new entries to a list. By clicking the button, a popup box displays a NewItem form of the list.

In order to allow users to have permissions to see the form and add an item to the list, I needed to apply the following steps.

1. Go to the list, select the “List” tab -> “List Permissions” from the ribbon.

2. Select “Stop Inheriting Permissions”

Stop Inheriting

3. Select the permission group and choose Edit User Permissions to give the group “Contribute” permission.

4. Go back to the list. Go to List Settings -> Advanced Settings

5. In the Item-level Permissions section, choose Create Items and edit items that were created by the user

Item-level

Display special character in SharePoint global navigation, quick launch and breadcrumb

On my SharePoint global navigation menu, quick launch or breadcrumb, for pages that has special character in their name, the characters are encoded. For example, “Health & Safety” displays as “Health &amp; Safety”, “Tomorrow’s Leader” displays as “Tomorrow#39;s”.

The solution for the global navigation is to add

EncodeTitle=”false” in the AspMenu control.

<!-- ------------| TOP NAVIGATION  |------------ -->
<div>
<!-- top navigation publishing data source -->
<SharePoint:AspMenu
   ID="TopNavigationMenuV4"
   Runat="server"
   EnableViewState="false"
   DataSourceID="topSiteMap"
   AccessKey="<%$Resources:wss,navigation_accesskey%>"
   UseSimpleRendering="true"
   UseSeparateCss="false"
   Orientation="Horizontal"
   StaticDisplayLevels="1"
   MaximumDynamicDisplayLevels="1"
   SkipLinkText=""
   EncodeTitle="false"
   CssClass="s4-tn"/>

<asp:SiteMapDataSource
   ShowStartingNode="False"
   SiteMapProvider="CombinedNavSiteMapProvider"
   id="topSiteMap"
   runat="server"/>
</div>
<!-- ------------| TOP NAVIGATION  |------------ -->
The solution for the quick launch is to add EncodeTitle=”false” in the quick launch control:
<SharePointWebControls:AspMenu id="V4QuickLaunchMenu" runat="server" EnableViewState="false" DataSourceId="QuickLaunchSiteMap" UseSimpleRendering="true" EncodeTitle="false" UseSeparateCss="false" Orientation="Vertical" StaticDisplayLevels="10" MaximumDynamicDisplayLevels="7" SkipLinkText="" CssClass="s4-ql noindex" />
The solution for the breadcrumb is different. We need to add javascript to do this. Here is the js code:
<script type="text/javascript">

    jQuery(document).ready(function () {

        fixSiteTitleBug();

    });

    function fixSiteTitleBug()
    {
        var searchAmp = '&amp;';
        var replaceAmp = '&';
        var searchQuote = '#39;';
        var replaceQuote = '\'';

        // Fix Quick Launch items
        var html = jQuery('#s4-leftpanel-content').html().replace(searchPattern, replacement);
        jQuery('#s4-leftpanel-content').html(html);

        // Fix breadcrumb items
        jQuery('a.SolidBreadCrumbRoot').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchAmp , replaceAmp ));
        });

        jQuery('a.SolidBreadCrumb').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchAmp , replaceAmp ));
        });

        jQuery('.SolidBreadCrumbCurrent').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchAmp , replaceAmp ));
        });

        jQuery('a.SolidBreadCrumbRoot').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchQuote, replaceQuote));
        });

        jQuery('a.SolidBreadCrumb').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchQuote, replaceQuote));
        });

        jQuery('.SolidBreadCrumbCurrent').each(function () {
            jQuery(this).html(jQuery(this).html().replace(searchQuote, replaceQuote));
        });
    }

</script>
Reference:

Add a printer friendly button to SharePoint publishing page

One of the request from my client is to allow user to print a page with only text content on it. This feature is quite common in many websites.

After reading Kathy Hughes “Microsoft SharePoint Designer 2010 Unleashed”, it’s pretty easy to do so in SharePoint 2010.

Scenario: I have 3 custom page layout for my site, home page layout, landing page layout and detail page layout. I only want to have a print button on pages that uses content page layout.

Here are the steps:

1. Create a new CSS file in the site collection’s Style Library and name it print.css. In this file, you can try hide all the components that you don’t want to see in the print view.

2. Open detail page layout. Add the following html inside the PlaceHolderMain tag.

< a id=”printer” onclick=”myPrinter(‘printer’)” href=”#” target=”_blank”><img src=”/Style Library/images/printer.png”></a>

3. On the same page, scroll to the top. Find the closing tag of PlaceHolderAdditionalPageHead. If you don’t have this tag, you need to add it first. Add the following JavaScript immediate before the closing tag. Of course you can add these code to a js file and add the link here instead.

<script type=”text/javascript”>
//if the url doesn’t contain printfriendly, use the normal style sheet
if (location.search.indexOf(‘printfriendly’) == -1){
document.write(‘<link rel=”stylesheet” type=”text/css” href=”/Style Library/css/gw_content_style.css”/>’);
}
//otherwise use the print style sheet and open the print window
else
{
document.write(‘<link rel=”stylesheet” type=”text/css” href=”/Style Library/css/gw_print.css”/>’);
window.print();
}

function myPrinter(id)
{
//if url doesn’t contain a question mark
if(location.search.indexOf(‘?’) ==-1)
{
var qrystr = document.getElementById(id).href;
//if  the url doesn’t contain a hash
if (qrystr.indexOf(‘#’) == -1)
{
//use the existing url.
//This means the button has been pressed before
//so the printfriendly parameter is already added.
document.getElementById(id).href = qrystr;
}
//otherwise the button hasn’t been pressed before
//so remove the # and add the printfriendly parameter
else
{
var newstr = qrystr.replace(‘#’, ‘?printfriendly=true’);
document.getElementById(id).href = newstr;
}
}
//this is used for pages like DisplayFullStory?ID=3 which has a parameter
else
{
var qrystr = document.getElementById(id).href;
if (qrystr.indexOf(‘#’) == -1)
{
document.getElementById(id).href = qrystr;
}
else
{
var newstr = qrystr.replace(‘#’, ‘&printfriendly=true’);
document.getElementById(id).href = newstr;
}
}
}
</script>

4. That’s it. When you test it, make sure you go to a page that uses the detail page layout.

 

Reference:

http://www.codingforums.com/archive/index.php/t-80517.html