Add Date Time automatically in SharePoint Blog Post

In this post we would be discussing on how we can disallow users to select the Published (Date-Time) field while creating a blog post in SharePoint. Like below:


Our aim is to set the Published automatically as per the current system's date-time.
If you are thinking to follow the usual way of hiding fields from the list form then you are not thinking about the impact it would have. I am talking about the below steps:

Go to the Advanced Settings of this list, set Allow Management of Content Types equal to Yes. Click on the Post content type, click on the Published column, and select Hidden.

The user will no longer have the option to set the published date/time. Note that it will still be set, but will be set the default current time.
The above two steps will land you in trouble and your blog's date-time will not be visible on blog post, even in list view. To tackle this issue there is a trick here to get the date-time on the post without Published field being available to the user.
Create a SPD workflow to publish on Post list. In this workflow simply create one step to update thePublished field value with Modified field value as show in below image. Ensure to only select "Start the workflow automatically when an item is created" at the Start Options in SPD.
Now create a post after publishing the workflow and you will see the date-time on your post and even in the list view with Published column being unavailable to the author.

Working with SharePoint 2010 Content Query Web Part & XSLT to create custom event date calendar

I was assigned to create an Event Calendar Display web part that may show employees leave plans in the IT department. I created the web part with the help of this awesome article http://labs.steveottenad.com/sharepoint-2010-xslt-date-formatting/ and I achieve what I needed. Later I felt the need of adding paging to the web part and started researching for it and came across below another awesome article references mentioned in point 3 and 5. Thanks to all the authors for doing this wonderful job.
  1. I Opened SPD and copied ContentQueryMain.xsl and ItemStyle.xsl from XSL Stylesheet folder to a custom folder, while keeping both default xsl files unchanged.
  2. I copied the xsl code and css code given in this article SharePoint 2010 XSLT date formatting to create the customized event calendar "Upcoming Events" (as shown in article) to my copied ItemStyle.xsl file. (Read the whole article and you will understand how you can perform this step).
  3. Download: ImtechContentQueryWebPart.wsp custom web part from this link:http://imtech.codeplex.com/ [This web part is a custom Content Query Web Part through which you can apply your own xsl files).
  4. Install the ImtechContentQueryWebpart to your SharePoint server and activate it through site collection features in Site Settings.
  5. Now go step by step and make changes again to your copied ItemStyle.xsl and ContentQueryMain.xsl files as explained in this article: Retro-fitting existing XSL Stylesheets with the Paging Controls
  6. The author in the article referenced in point 5 has pasted the code below <xsl:template name="Default" match="*" mode="itemstyle"> Instead a I posted the code below this line<xsl:template name="EventDisplay" match="Row[@Style='EventDisplay']" mode="itemstyle">The above line having EventDisplay you will find in your copied & edited ItemStyle.xsl file. Simply search the term EventDisplay and you will get it.
    Finally insert the Imtech Content Query Web Part to your page and copy the path of copied ItemStyle.xsl and ContentQueryMain.xsl files. Paste these paths as shown below in first image:
    enter image description here
    And the final result you get is: [*Please note the Leave Type and Click Here is appearing due to my own customized code in ItemStyle.xsl. Rest the output is same as shown in article referenced in point 2.]
    enter image description here

I am posting my retrofitted custom ItemStyle.xsl which I managed to create with above steps. I am posting an extract of my ItemStyle.xsl below that would help in adding Leave Type and Click Here.
-First create a Leave Type drop-down column in your SharePoint Calendar List and also create a simple text column CalendarLink having a default value as "http://yoursiteURL,Click Here". This value would be created for every item you enter in SharePoint Calendar List. In Leave Type column enter any values like Annual Leave, Medical Leave or just anything of your choice.

If you observe my below code attentively then you will make out how I have referenced Leave Type and CalendarLink columns in the code. If you have any doubts or face any challenge, feel free to leave a comment and I will surely love to respond.

You can even copy my below code to your custom ItemStyle.xsl and get the job half done. Rest you should take care of ContenQueryMain.xsl, and Custom Columns.

Thank you for reading this article and making my effort feel worth it !


<xsl:template name="EventDisplay" match="Row[@Style='EventDisplay']" mode="itemstyle">  
   <xsl:param name="CurPos" />  
      <xsl:variable name="dateTime" select="ddwrt:FormatDate(string(@EventDate), 1033, 3)" />  
      <xsl:variable name="dateTimeCondensed" select="ddwrt:FormatDate(string(@EventDate), 1033, 2)" />  
      <xsl:variable name="date" select="substring-before(substring-after($dateTime, ', '), ', ')" />  
      <xsl:variable name="month" select="substring-before($date, ' ')" />  
      <xsl:variable name="day"  select="substring-after($date, ' ')" />  
      <xsl:variable name="time" select="substring-after($dateTimeCondensed, ' ')" />  
      <xsl:variable name="SiteLink" select="substring-before(@CalendarLink,',')"/>  
   <xsl:variable name="SiteName" select="substring-after(@CalendarLink,',')"/>  
   <xsl:variable name="LeaveData" select="concat($SiteLink,'/dispform.aspx?ID=',@ID)"/>  
      <xsl:variable name="SafeLinkUrl">  
           <xsl:call-template name="OuterTemplate.GetSafeLink">  
              <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>  
           </xsl:call-template>  
      </xsl:variable>  
      <div class="item link-item">  
           <div class="description xslCal">  
                <div class="left">  
                     <span class="month"><xsl:value-of select="$month" /></span>  
                     <span class="day"><xsl:value-of select="$day" /></span>  
                </div><!--/Left-->  
                <div class="right">  
                     <span class="title"><xsl:value-of select="@Title" /></span><br/><br/>  
               <span class="Category">Leave Type: <xsl:value-of select="@Category" /></span>  
             </div><!--/Right-->  
           </div><!--/description-->  
      </div><!--/item-->  
      <a href="{$LeaveData}"><xsl:value-of select="$SiteName"/></a>  
        <xsl:if test="$PageSize &gt; 0">  
          <xsl:variable name="NumPages" select="ceiling($TotalRecords div $PageSize)"/>  
          <xsl:variable name="PageBeforeLast" select="$NumPages - 1"></xsl:variable>  
          <xsl:variable name="CountBeforeLastPage" select="$PageBeforeLast * $PageSize"></xsl:variable>  
          <xsl:variable name="LastPageCount" select="$TotalRecords - $CountBeforeLastPage"></xsl:variable>  
            <xsl:if test="$PageNumber = $NumPages and $CurPos = $LastPageCount">  
               <br/>  
               <xsl:call-template name="PagingControls">  
                <xsl:with-param name="Page" select="1" />  
                <xsl:with-param name="NumPages" select="$NumPages" />  
               </xsl:call-template>  
            </xsl:if>  
            <xsl:if test="$CurPos = $PageSize and $NumPages &gt; 1 and $PageNumber != $NumPages">  
             <br/>  
             <xsl:call-template name="PagingControls">  
              <xsl:with-param name="Page" select="1" />  
              <xsl:with-param name="NumPages" select="$NumPages" />  
             </xsl:call-template>   
          </xsl:if>  
        </xsl:if>  
      </xsl:template>  
      <xsl:template name="PagingControls">  
   <xsl:param name="Page" />  
   <xsl:param name="NumPages" />  
   <xsl:if test="$Page = 1">  
    <xsl:text disable-output-escaping="yes"><![CDATA[<div>]]></xsl:text>  
    <xsl:if test="$PageNumber &gt; 1">  
     <xsl:variable name="PreviousPageNo" select="$PageNumber - 1"/>  
     <a href="?{$PagingParameterName}={$PreviousPageNo}">  
      <xsl:text disable-output-escaping="yes"><![CDATA[&laquo;]]></xsl:text> Prev  
     </a>  
     <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>  
    </xsl:if>  
   </xsl:if>  
   <xsl:choose>  
    <xsl:when test="$Page = $PageNumber">  
     <strong>  
      <xsl:value-of select="$Page"/>  
     </strong>  
    </xsl:when>  
    <xsl:otherwise>  
     <a href="?{$PagingParameterName}={$Page}">  
      <xsl:value-of select="$Page"/>  
     </a>  
    </xsl:otherwise>  
   </xsl:choose>  
   <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>  
   <xsl:if test="$Page &lt; $NumPages">  
    <xsl:call-template name="PagingControls">  
     <xsl:with-param name="Page" select="$Page + 1"/>  
     <xsl:with-param name="NumPages" select="$NumPages" />  
    </xsl:call-template>  
   </xsl:if>  
   <xsl:if test="$Page = $NumPages">  
    <xsl:if test="$PageNumber &lt; $NumPages">  
     <xsl:variable name="NextPageNo" select="$PageNumber + 1"/>  
     <a href="?{$PagingParameterName}={$NextPageNo}">  
      Next  
      <xsl:text disable-output-escaping="yes"><![CDATA[&raquo;]]></xsl:text>  
     </a>  
    </xsl:if>  
    <xsl:text disable-output-escaping="yes"><![CDATA[</div>]]></xsl:text>  
   </xsl:if>  
  </xsl:template>  



Hide a field from SharePoint Designer Form - NewForm/DisplayForm/EditForm

To hide a field from any of the SharePoint Designer Form we usually create a custom form and edit the code and add a style attribute as style="display:none" in <tr> element.

But what if you do not want to create a custom form and hide the field on the default form? Is it possible? The answer is YES.

You can hide the field on default SPD form via PowerShell command without having to create a custom form.

Open SharePoint Management Shell and type the following commands. The below example hides a field from a NewForm.

$web = Get-SPWeb webUrl
$list = $web.Lists.TryGetList("ListName")
if($list)
{
    $field = $list.Fields["FieldName"]
    $field.ShowInNewForm = $false
    $field.Update()
}
$web.Dispose()

You can similarly execute the below command for DisplayForm and EditForm. The only statement you need to change is:
$field.ShowInDisplayForm = $false
$field.ShowInEditForm = $false

Identify the type of SharePoint List Template

There are three methods that I know to identify the type of SharePoint List template. All the methods has easy steps to execute and find the type:

     1. Using Browser Method
·         Open that SharePoint List which you want to identify its type and ensure to open that list in AllItems.aspx view. Then in your web browser click on the menu bar > View > Source or simply right click on the page and select View Source and search for ctx.listTemplate.
·         Once you find ctx.listTemplate then match its value with the available List Template IDs (Click Here).

     2. Using PowerShell Command
·         Connect to your SharePoint Server and open SharePoint 2010 Management Shell.
·         Type the following commands that would retrieve all list and libraries in a web site. Search the name of your target list/library and match the Type_Client with List Template IDs given in above link.

>> $SPWeb = Get-SPWeb “http://yourSharePointServer/
>> $SPWeb.ListTemplates | Select Name, type, type_client, Description

Example:




     3. Using XML
·         Go to List/Library Settings and from the browser URL copy the string that appears after List=
·         Open this web site http://meyerweb.com/eric/tools/dencoder/ and paste the copied string and press Decode button.
·         Now copy the decoded string paste it somewhere in notepad.
·         Get the SharePoint web site URL under which the list/library is created.
·         Now construct the URL as below example:

·         In the above URL all remain same except the website URL before /_vti_bin/ and copy your decoded List ID and put it inside the braces { } after List=
·         Then finally copy the entire constructed URL and paste into IE and press Enter.
·        In the XML returned by browser search for text ServerTemplate and match its value with the List Template ID, link provided in point 1.

Picture Library SlideShow Web Part not displaying image in original size

This is a known fact that out-of-the-box Picture Library SlideShow Web Part does not display the images in its original size. Further more, unfortunately it is not supported to configure in web part what image type (original, web or thumbnail) should be displayed in Slideshow.

The JavaScript code described below allows to display original images (without re-sizing) in Slideshow Web Part. In order to display original image the technique is to override the ShowPic function. For embedding JavaScript on page, use Content Editor web part (CEWP) on the same page where you have also inserted your SlideShow web part.

<script type="text/javascript">


function SlideshowObjectInitializer() {

  ShowPic = (function(ShowPicOrig) {
      return function() {

           var ssObj = arguments[0];  //SlideShow object
           var curPicIdx=ssObj.index; //current picture index

           ShowPicOrig.apply(this, arguments); //call original ShowPic

           //apply some changes to display original picture in SlideShow control
           ssObj.image.src = ssObj.linkArray[curPicIdx]; //display original image instead of web image
           //change picture & container size to auto instead of fixed (by default web image size is used)
             ssObj.image.setAttribute('height','100%'); 
             ssObj.image.setAttribute('width','100%'); 
             var cell = ssObj.cell; 
             cell.style.width = 'auto';
             cell.style.height = 'auto';
             cell.style.display = '';
             var pcell = ssObj.cell.parentNode; 
           pcell.style.width = 'auto';
             pcell.style.height = 'auto';
      };
  })(ShowPic);

}  


ExecuteOrDelayUntilScriptLoaded(SlideshowObjectInitializer, 'imglib.js');
</script>
In the above given code you can adjust the size by modifying these lines/values:
ssObj.image.setAttribute('height','100%'); 
ssObj.image.setAttribute('width','100%');
The value '100%' can be changed to '90%' or '80%' and even in pixels you can mention the size, example:
ssObj.image.setAttribute('height','400'); 
ssObj.image.setAttribute('width','700');