Sunday, July 31, 2011

Add user control in sharepoint master page

Add user control in sharepoint master page

06/27/2010Admin  
Category: SharePoint Programming
In this post we will see how to add a user Control in a SharePoint Master Page, along with its code file. Here we will create a user control with a hyperlink, and will change the title of the hyperlink and its navigation url in the page load of the code file.
The steps are:
1. Create the Web user Control (ascx) along with the code file.
Open Visual Studio and Create a new web application project.

Add a Web User  Control in your project.

In the ascx file add a hyperlink control.( like below)

Now, lets add few lines in our code behind file. See the below image.

Ok we are done adding the stuff. Now lets build the Project. Oh wait! we didn’t sign the project, since we are going to throw the .dll for this project  into GAC, we need to sign the project with a key.

2. Signing the Project  -

firstly, let create a key that we need for signing. To do this browse to the VS tools command prompt (See below ).

Now, create the key using the command below.

Now build the Project.
After you have done this, we need to make one little modification to the ascx file so that it uses what we have written in the code behind. We usually do not deploy .cs or code files along with our ascx or aspx pages in Sharepoint, instead the ascx file accesses the code file, by inheriting from the .dll of the project. so lets change the Inherit tag in the ascx file to something like below :
Inherits=”MasterPageUserControlDemo.MasterPageUserControl1, MasterPageUserControlDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0745576964bce313″
Now build the Project once again.
3.  Deploy the Control -
To add the control in SharePoint context, Open the project folder and just  drag and drop the control into controltemplates folder of SharePoint 12 hive.
I mean add this under this path : “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES”
Next, drag and drop your signed assembly into GAC or C:\WINDOWS\assembly folder.

4.  Now, we are ready to add our user control in our master page -

So, lets start with registering the user control in the master page . To do this, open your SharePoint site in SharePoint designer and check out the master page.  Now, add the line below
on the top the page. See the Image below.
<%@ Register TagPrefix=”ouruc” src=”~/_controltemplates/MasterPageUserControl1.ascx” TagName=”OurSitesLink” %>

Now lets just add the control anywhere on your page.  I added the below line in the footer of the page . See the image below.
&gt;ouruc:OurSitesLink id=”Footerlink” runat=”server”/&lt;

Now check in the page and approve it.
The Result should be something like below.

Tuesday, July 19, 2011

Create a Polling Web Part with SharePoint Designer and the DVWP

Create a Polling Web Part with SharePoint Designer and the DVWP


Guest Author: PatCharles Iovanella
As a continuation of my Best of EUSP article I was asked to expand on the Polling Data View Web Part.  As I stated in my article, �SharePoint surveys leave a lot to be desired, especially from the end user perspective. What if you want to ask only one question and do not want users to leave the current page?  A SharePoint survey is not the answer. But you can use the SharePoint survey and create a custom polling web part via SharePoint Designer and the Data View Web Part. You can turn a one question SharePoint survey into a cool looking Polling Web Part.�
So let�s get to it!  I�m making a few assumptions that you know how to use SharePoint Designer and are able to complete all the steps without any further elaboration.
  1. Create your one question SharePoint survey
  2. Create a new Demo page using the Web Part Page template. This page will be our sandbox for building the polling web part.
  3. Open SharePoint Designer and then open your new Demo page
    1. Click into any Web Part Zone
    2. Insert Data View Web Part- Data View - Insert Data View
    3. Select the correct Survey Data Source
    4. Select the one question field
    5. Insert Selected Fields - New Item Form
    6.  
    7. Highlight both columns together, right click, Modify - Merge Cells
    8. So, now we have consolidated our survey into a small compact DVWP.  So let�s create a voting button.
    9. Delete the Cancel button and double click on the Save button
    10. Change the Value/Label to �Vote�
    11. Save Demo page and view in browser
  4. From the Demo page, export the Polling Web Part to your desktop
  5. Import the Polling Web Part and add to your page.
  6. Done!
Now, if you want to take it a step further and add the Graphical Summary responses, you can do that too. I used Christophe Humbert�s Cross List jQuery script to expose the SharePoint survey summary.aspx on the same page as my poll web part. This way every time some takes the poll the page refreshes and displays the real-time poll results. Please refer to Christophe�s instructions in the links provided.

http://blog.pathtosharepoint.com/2009/03/23/display-a-list-in-another-site-contd/
http://www.pathtosharepoint.com/sharepoint-user-toolkit/Pages/Cross-Site-List-Snapshot.aspx

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

Friday, July 15, 2011

Make Title the Link Column in a Document Library and Add the !NEW Icon

http://www.thesug.org/blogs/SharePointUnwrapped/Lists/Posts/ViewPost.aspx?ID=6

We all know that new documents added to a library automatically get the !NEW icon listed with the name (filename).  But what if you want the !NEW icon displayed with another column value or want to use the more useful Title column as the document link instead of file name?

The file name is what links to the document by default, not the title, and that's not always what we prefer to display.  This thread walks you through changing the link from Name to Title and adding the !NEW icon to the Title instead of the default Name field.

First, create the view you want for your library, such as adding Title to be a display column and possibly removing Name from the view.  For this example, let’s modify the All Documents view on that library to include the Title column.  In SharePoint Designer, go into the Forms folder for the library and open the AllItems.aspx file (or the view you’ve created).  This file manages the specific view created and each view will have its own file.  The image below shows the AllItems.aspx page (the All Documents view) from within the document library named test.
 

 
In order to modify the view’s headings, you must convert the view to XSLT data view.  With Designer in split mode, right-click on the view to bring up the menu shown below, and click on Convert to XSLT Data View.
 

 
Once the conversion is complete, click on the Title filed to find the code that displays that field.  Add the following code per the screenshot below to add the !NEW icon to the Title field:

<xsl:if test="ddwrt:IfNew(string(@Created))"><img src="/_layouts/1033/images/new.gif" alt="New" /></xsl:if>

If you left the Name column in the view, this code will already exist with that heading, so simply cut from there and past into the Title heading as shown below.

Custom Date Formats in SharePoint XSL

http://blogs.msdn.com/b/joshuag/archive/2009/03/25/custom-date-formats-in-sharepoint-xsl.aspx

There are quite a few posts out there on this topic, but I’m yet to find one comprehensive post that walks through this beginning to end and actually works.  Let’s give it a go.
A very common scenario for SharePoint publishing sites is to customize the look to suit the customers needs.  Usually this is done with a Content Query Web Part and some custom XSL.  When doing this very often you need to display a date.  You will quickly notice that just displaying the date that SharePoint gives you is not going to be sufficient.  If you just did the standard

<xsl:value-of select="@ArticleStartDate"/>
You get back a pretty nasty looking result
2009-03-23 00:00:00

However if you use the “FormatDate” function, you can make this look a lot better.
<xsl:value-of select="ddwrt:FormatDate(@ArticleStartDate, 2057, 3)"/>
Results in this
23 March 2009
All you need to do to make sure the “FormatDate” function is available in your custom XSL files is to make sure you reference the ddwrt namespace.
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime
Once this has been added to the rest of your namespace declarations at the top of your <xsl:stylesheet> tag, you should be able to use the “FormatDate” function anywhere you like.  Here is sample of what a full XSL file would look like that does this.
<xsl:stylesheet 
version="1.0" 
exclude-result-prefixes="x d xsl msxsl cmswrt"
xmlns:x="http://www.w3.org/2001/XMLSchema" 
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" 
xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
<xsl:template name="Default" match="*" mode="itemstyle">
<xsl:value-of select="ddwrt:FormatDate(@ArticleStartDate, 2057, 3)"/>   
</xsl:template>
</xsl:stylesheet>
Here are the details on the different formats you can get by changing the parameters.  March 23 12:00 AM was used as input for all outputs.
OutputLocaleFormat
3/23/200910331
3/23/2009 12:00 AM10332
Monday, March 23 200910333
12:00 AM10334
Monday, March 23, 2009 12:00 AM10337
3/23/2009 12:00:00 AM103313
Monday, March 23, 2009 12:00:00 AM103315
23/03/200920571
3/23/2009 12:00 AM20572
23 March 200920573
00:0020574
23/03/2009 00:0020575
23 March 2009 00:0020577
00:00:00205712
23/03/2009 00:00:00205713
23 March 2009 00:00:00205715
You can also get a list of all the available locale’s here.