Friday, June 1, 2012

XSLT template display part of article as summary

When created news, articles, and etc..., I want to take part of article as summary displayed in listing and headlines.I Created XSLT which can be used for this functionality:


<xsl:template name="trimPara">
    <!-- pass the string to be cropped with required string length -->
    <xsl:param name="stringLenRequired"/>
    <xsl:param name="stringInput"/>
    <xsl:value-of disable-output-escaping="yes" select="substring($stringInput,1,number($stringLenRequired))"/>
    <xsl:value-of disable-output-escaping="yes" select="substring-before(concat(substring($stringInput,number($stringLenRequired)+1,number($stringLenRequired) +20),' ' ),' ')"/>
    <!-- to make it a full word -->
    <xsl:if test="string-length($stringInput) > number($stringLenRequired)">...</xsl:if>
  </xsl:template>



You can call this template by the following:


   <xsl:call-template name="trimPara">
            <xsl:with-param  name="stringLenRequired">200</xsl:with-param>
            <xsl:with-param name="stringInput" select="@Body"/>
          </xsl:call-template>
        </div>


Access WebParts using HttpContext from Console Application

I Created Console Application to export WebParts from SharePoint but I faced problem with web parts used HttpContext like CQWP (With Console Application HttpContext.Current is null). I cannot read web part properties. After some investigation I found the solution create a dummy HttpContext with the following code:


  if (HttpContext.Current == null) {
      HttpRequest request = new HttpRequest("", web.Url, "");
      HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
      HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
  }

Don't forget to set HttpContext.Current to null after finishing.
Now I can access web parts from console application.

Cannot Convert Type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor – Beware of this in custom master page for custom publishing site definition

When you create site definition or custom master page as feature you may face error like:

Cannot Convert Type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor – Beware of this in custom master page for custom publishing site definition.
Reason:
When you export master page through SharePoint designer or WebDAV, It will add designer attributes (__designer) which will cause these problem.
<SharePoint:FieldValue FieldName="Title" runat="server" __designer:Preview="XXXXX" __designer:Values="XXXXX"/>

Also when open custom physical master page in SharePoint designer it adds __designer:Preview and __designer:Values attributes to my FieldValue and Literal controls, and if I don't remove them before check in ASP.NET complains.

 Also It may occur when you use html tags as below:

<a href="<%$SPUrl:~sitecollection/Pages/YourLink.aspx%>" >View All</a>

check url:
http://vyeung.wordpress.com/2011/06/01/sharepoint-designer-2010-addinggenerating-__designer-preview-tags-a-fix-to-solve-this-issue/


Solutions:
  1. Copy content instead of export from SPD.
  2. Double Check file and make sure you removed these attributes (__designer) 
  3. Look at the page directives in the top of the file, and ensure that a tilde and forward slash are present in the path(s):
    <%@ Register TagPrefix="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>

Use Content Query WebPart with site definition

When you want to create site using custom site definition and want to use Content Query WebPart, Export definition of your web part from existing site. You will find list is defined by Id called ListGuid. But the ListGuid is never the same when you create a new List. You need to do the the following steps before adding exported web part to your onet.xml:
  1. Remove ListGuid
    <Property name="ListGuid" type="string"></Property>
  2.  Add ListName and WebUrl (if they are not exist)
    <Property name=”ListName” type=”string”>Posts</Property>
    <Property name=”WebUrl” type=”string”>~site/</Property>
Note:
  1. ~site represent current site. you can use ~sitecollection to represent current site collection.
  2. ~site/ I added slash because if we are in root WebUrl will be blank and CQWP will not be rendered.

Monday, May 14, 2012

Add Lync Presence to SharePoint List column with XSL Stylesheet

If you want display Lync link with your contact list.
  1. Add column to the list Single line of text will call Presence.
  2. Edit Contact List Web Part Set XSL Link Property with new xsl.
  3. Create a XSL file with a style sheet, this style will add presence based on the Email field in the Contact list.
    <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office">
     <xsl:include href="/_layouts/xsl/main.xsl"/>
     <xsl:include href="/_layouts/xsl/internal.xsl"/>
     <xsl:template name="FieldRef_Text_body.Presence" match="FieldRef[@Name='Presence']" mode="Text_body">
      <xsl:param name="thisNode" select="."/>
      <xsl:variable name="etternavn">
       <xsl:value-of select="$thisNode/@Title" />
      </xsl:variable>
      <xsl:variable name="fornavn">
       <xsl:value-of select="$thisNode/@FirstName" />
      </xsl:variable>
      <xsl:variable name="fulltNavn">
       <xsl:value-of select="concat($fornavn,' ',$etternavn)" />
      </xsl:variable>
      <xsl:variable name="itemID">
       <xsl:value-of select="$thisNode/@ID" />
      </xsl:variable>
     
      <xsl:variable name="sipAfter">
       <xsl:value-of select="substring-after($thisNode/@Email,':')" />
      </xsl:variable>
      <xsl:variable name="sip">
       <xsl:value-of select="substring-before($sipAfter,'"')" />
      </xsl:variable>
     
       <div id="PresenceLink_{$itemID}"><span class="ms-imnSpan"><img border='0' height='12' src='/_layouts/images/imnhdr.gif' onload="IMNRC('{$sip}')" ShowOfflinePawn='1' style='padding-right: 3px;' id="PresencePawn_{$itemID}" alt='pawn' /> <a href='' id="ProfileLink_{$itemID}"><xsl:value-of select="$fulltNavn" /></a></span></div>
     </xsl:template>
    </xsl:stylesheet>

Tuesday, May 8, 2012

SharePoint Software Factory 2010

I found a free Visual Studio Extension helping SharePoint newbies, as well as experienced developers to create, manage and deploy SharePoint solutions without having to know every tiny XML and C# secret.

Key Features:
  • Get started with professional SharePoint development in less than 5 mins!
  • Use powerful wizards to create all important SharePoint artifacts
  • Fully integrated in Visual Studio
  • Refactoring of artifacts (i.e. Content Types)
  • Fully automated build system creates web solutions packages (“WSP”s) and deployment files 
  • Integrated Code quality checks in “Release” build (additional installs required)
  • All projects are self maintained, meaning that there is no dependency to having SPSF installed on the machine, when you just want to build, or continue development without SPSF
  • Standard VS2010 item templates for SharePoint development can be used together with SPSF projects, though the coding conventions have to be applied by the developers for these items
  • Extensive Help integrated in Visual Studio
  • Supports new SharePoint Visual Studio application structure but also “old” best-practice SharePoint Hive structure
  • Supports SharePoint 2007/2010 and Visual Studio 2008/2010
  • Upgrade existing SharePoint projects.
You can check this extension from link:
http://spsf.codeplex.com/

SharePoint Best Practices when use object model

When we run SharePoint Site Without any code customization we will notice big difference in performance. There are many common things affect performance with object model:

  • Dispose SharePoint Objects:
    SharePoint object model contains objects that implement the IDisposable interface. You must take precautions when using these objects to avoid their long-term retention in memory in the Microsoft .NET Framework.
    Specifically, you should explicitly dispose of those SharePoint objects that implement IDisposable when you are finished using them.
    The following unusual behaviors occurred when you did not disposing of SharePoint objects when you are finished with them

    1. Frequent recycles of the Windows SharePoint Services application pool, especially during peak usage.
    2.  Application crashes that appear as heap corruption in the debugger.
    3.  High memory use for Microsoft Internet Information Services (IIS) worker processes.
    4. Poor system and application performance.

    There are 2 articles contains best practice for disposing objects:
    http://msdn.microsoft.com/en-us/library/ee557362.aspx

    http://blogs.msdn.com/b/rogerla/archive/2009/11/30/sharepoint-2007-2010-do-not-dispose-guidance-spdisposecheck.aspx

You can use SharePoint Dispose Checker Tool:
This tool helps developers and administrators check custom SharePoint solutions that use the SharePoint Object Model helping measure against known Microsoft dispose best practices.
http://archive.msdn.microsoft.com/SPDisposeCheck

  • Using SharePoint Data and Objects Efficiently

    1. Caching Data and Objects 
    2. Using Objects in Event Receivers
    3. Working with Folders and Lists
    4. Deleting Multiple Versions of a List Item
    5. Writing Applications That Scale to Large Numbers of Users

    There is article take about Common Coding Issues When Using the SharePoint Object Model:  http://msdn.microsoft.com/en-us/library/bb687949%28v=office.12%29.aspx
  • Decrease round trips with the database:|
    When you access SharePoint using object model every time access . retrieve data from the database for example:
    If we loop over list items like:
    for(int i=0;i&lt;web.list["News"].Items.Count;i++)         
    {
          SPListItem item = web.list["News"].Items[i];
    }

    The best practice assign SharePoint Objects in variables and use these variable:
    SPList list = web.list["News"];
    SPListItemCollection itemCol= list.Items;                       
    for(int i=0;i&lt;itemCol.Count;i++)
    {
          SPListItem item = itemCol[i];
    }
    This role must be applied in most objects of SharePoint.
I found good engine called FASTSPWeb in codeplex help you to solve some problems:
  • Decrease database round trip by caching SharePoint objects automatically in the web application process and allowing multithreaded access to these objects.
  • Correct disposing of opened SPWeb and SPSite object is also not an issue because FASPSPWeb is handling all instantiating and disposing automatically.
This tool link is:
http://fastspweb.codeplex.com/