Wednesday, December 5, 2012

Javascript to get query string value by passing the parameters siteUrl and querystringkey



In this post we will be writing a function to retrieve the query string value by passing the parameters siteUrl and querystringkey using Javascript.

Below is the function:
function getQuerystringForUrl(key,siteUrl) {   
    key = decodeURIComponent(key).replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + key + "=([^&#]*)";
    var regex = new RegExp(regexS);           
    var results = regex.exec(siteUrl);
    if (results == null)
        return;
    else
        return decodeURIComponent(results[1]);
}


Hope this helps...

Tuesday, December 4, 2012

How to Add a Favicon to a SharePoint Site or any regular website

In this post we are going to Add a Favicon to a SharePoint Site or any regular website.



A favicon is an image(.ico extension) associated with a particular Web page or a complete Web site. Favicon (pronounced fav-eye-con) is short for 'Favorites Icon.'
In Internet Explorer the Favicon is displayed on the Address bar and in the Tab.

Below are the steps to achieve this.
Step1:
Create a .ico image file with the below link by uploading your Logo.
Once the .ico file is created save it as favicon.ico and make sure it is of 16*16 Pixels.

Step2:
Adding the favicon reference to our website.
Add the below reference in the <head> section of your master page so that favicon will be displayed across the website. Or if you want to show it on particular page only, then add it to respective page itself.
<link rel="shortcut icon" href="your_path_to_favicon/favicon.ico" type="image/vnd.microsoft.icon" />

In Sharepoint to achieve the same we can use either the above code snippet or we can make use of below Sharepoint control SPShortcutIcon.
<SharePoint:SPShortcutIcon ID="SPShortcutIcon1" runat="server" IconUrl="/_layouts/images/favicon.ico"/>

You can give the IconUrl reference by placing the image either in the 14 hive or in the Image Library of your site collection. Both will workout.


Hope this helps...

Implenting Qualaroo Survey to your website


In this post we are going to add a Survey to our website with minimal maintenance intervention.


      Below are the steps to implement Qualaroo Survey on your page

      1)      We need to create an account under Qualaroo.
Account Types-
a) Free User
b) Premium user
For more info please ref https://qualaroo.com/plans
      2)      Login via https://qualaroo.com/signin
      3)      Upon successful login we will now be taken to dashboard of Qualaroo.
      4)      Click on the Create a new survey button which will be present in dashboard.
      5)      We will be redirected to a page where will have some Predefined Surveys and we can choose any one of them.
      6)      To create a custom survey Click on the Create a new survey button.
      7)      We will be redirected to new survey form where we need to enter the information like Survey name, question, answers, answer type(radio button, checkbox) etc.,
      8)      We can also add multiple questions. Finally after adding all the questions Click on Save Survey button.
      9)      After successful save we will be redirected to Configure survey page, where we will have multiple options like Url of the site where it is targeted, Display survey From date- To date, User targeting etc.,
      10)   Enter the mandatory fields and click on Save Survey configuration.
      11)   Survey will now be ready and we can preview it also by clicking on Preview.
      12)   Now Go back to DashBoard and we should see our newly created Survey.
      13)   There will be a button called Install Code corresponding to our survey. Click on it to get the code which needs to be incorporated on our site.
      14) Copy the generated code just below <body> tag to the page of the site to which you want to display this survey.

      That’s it you would be able to see the Survey at the bottom right corner of your page.Below fig shows the sample survey.
      
















      Hope this helps...
   



Wednesday, September 26, 2012

How to use XSLT in sharepoint 2010


In this post we are going to see how to use XSLT in Sharepoint 2010, to display the list data in a customized way on a webpart page.
This article will be helpful for those who are just starting with XSLT in MOSS 2010.

Consider the below list EmployeeDetails







At the end of this post we will be displaying the above list data as below in the web part page.
Expected Result:











Step1:
To start with, whenever we talk about creation of XSLT we must be ready with the HTML markup how we want to display.
In order to achieve the above expected result I have created the HTML markup as below.

<table border="1">
        <tr>
            <td rowspan="3" valign="top" width="30%">
                <img src="" />
            </td>
            <td style="text-align: left" valign="top" width="70%">
                Name
           </td>
        </tr>
        <tr>
            <td style="text-align: left" valign="top" width="70%">
                Skills
            </td>
        </tr>
        <tr>
            <td style="text-align: left" valign="top" width="70%">
                Summary
            </td>
        </tr>
</table>

Step2:
Now let’s start with the creation of XSLT. The above highlighted yellow fields will be replaced by XSL values of the fields.
Lets create the below XSLT Template with the name MyCustomStyle.

<xsl:template name="MyCustomStyle" match="Row[@Style='MyCustomStyle']" mode="itemstyle">
    <table border='1'>
      <tr>
        <td rowspan="3" valign="top" width="30%">
          <img>
            <xsl:attribute name="src">
              <xsl:value-of select="substring-before(@UserImage,',')" />
            </xsl:attribute>
          </img>
        </td>
        <td style="text-align: left" valign="top" width="70%">
          <h3>
            <xsl:value-of select="@UserName" />
          </h3>
        </td>
      </tr>
      <tr>
        <td style="text-align: left" valign="top" width="70%">
          <xsl:value-of disable-output-escaping="yes" select="@UserSkillset" />
        </td>
      </tr>
      <tr>
        <td style="text-align: left" valign="top" width="70%">
          <xsl:value-of disable-output-escaping="yes" select="@UserSummary" />
        </td>
      </tr>
    </table>
</xsl:template>

Step3:
Now we are going to add this XSL Template to a existing XSLT(ItemStyle) present in XSL Style Sheets which is present under Style Library.
Download the ItemStyle xslt and add the above template at the end of the style sheet.
You can even create a new XSLT rather than adding in any existing one.

Step4:
With the above steps we are ready with our XSLT changes and ready to add this to a webpart page.
      A)     Create a webpart page.
      B)      Add a Content Query Webpart.
      C)      Open the tool pane of the above webpart(Edit Webpart).
      D)     Select the list EmployeeDetails in the source section.
      E)      Expand the Styles Section which is present under Presentation Group.
      F)      In the Item Style Drop down, you will be able to see the XSLT template name MyCustomStyle and select it.
      G)     Once after you select our custom created style, You must be able to see the green colored highlighted variables present in XSLT in the Fields to display section.
      H)     Add the respective Field internal name to the respective Variables.
       I)        Finally Click Apply and Press Ok button.

Your expected result will be available now.


Hope this helps...



Monday, September 3, 2012

What is a Property Bag in sharepoint 2010


In this post we are going to discuss what is Property Bag in sharepoint 2010.

Basically this property bag is a feature available in Windows SharePoint Services 3.0.
It is a Hash Table where in we will maintain key-Value information. The basic fundu of this Property bag is to add the properties to the objects of the SharePoint site.

Why Property bag instead of web.config?
While defining the property bag we said it is an Hash Table where we will maintain Key-Value information. The question arises here is, when we can maintain key-value information in web.config(appsettings section-Ref below appsettings) why should we go for Property bag.

<configuration>
    <appSettings>
        <add key="MyKey" value="MyValue" />
    </appSettings>
</configuration>

And the answer is, When we add key-value in web.config which will become common to the entire web application.

What, if there is any setting specific to individual site in the web application?
In this scenario we will have to adopt either of the below cases.
è        1) Maintain multiple key-value entries in web.config.
è        2) Use sharepoint property bag. With this we can store the properties in several levels like SPFarm, SPWebApplication, SPSite, SPWeb and SPList.

Property Bag in sharepoint 2010:
There is no specific user interface available to add/modify the property bag settings. We have to do it through object model.
Also there is an option available in SharePoint designer to add/modify the property bag settings. Go to Site -> Site Settings. click on the Parameters tab.  On this tab, you will be able to manipulate all of your custom property bag values.

Add/Modify Property bag with Object model:
In the below example we are adding/reading/updating/deleting the properties at the farm level. Similarly we can do it for rest all the scopes mentioned above.

Adding:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties.Add("myFarmKey", "myFarmValue");
myFarm.Update();

Reading:
SSPFarm myFarm = SPFarm.Local;
if (myFarm.Properties != null && myFarm.Properties.Count > 0)
{
if (myFarm.Properties.ContainsKey("myFarmKey"))
{
String myFarmValue= myWebApplication.Properties["myFarmKey"];
}
}

Updating:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = "myNewFarmValue";
myFarm.Update();

Deleting:
SPFarm myFarm = SPFarm.Local;
myFarm.Properties["myFarmKey"] = null;
myFarm.Properties.Remove("myFarmKey");
myFarm.Update();


Hope this helps…

Wednesday, March 7, 2012

Programatically set the welcome page of a sharepoint site MOSS2010

In this post we will see how to change the welcome page of a sharepoint site using object modelling.

Normally for any sharepoint site, the welcome page will be default.aspx. In this post we will set the Home.aspx page(present in pages library) as the welcome page, so that home.aspx will open as soon as user opens the site.

Below is the code snippet::

        /// <summary>
        /// method to add welcome page
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="oWeb"></param>
        /// <param name="pageName"></param>
        private void CreateWelcomePage(PublishingWeb publishingWeb, string pageName)
        {
            try
            {
                string fullPageUrl = publishingWeb.Url + "/Pages/" + pageName + ".aspx";
                SPFile fileNew = publishingWeb.Web.GetFile(fullPageUrl);
                publishingWeb.DefaultPage = fileNew;
                fileNew.Publish(pageName + ".aspx set as Welcome Page");
                fileNew.Approve(pageName + ".aspx set as Welcome Page");
                publishingWeb.Update();
            }
            catch (Exception ex)
            {
               
            }

        }

Call the above method as below
CreateWelcomePage(myPublishingWebObj,"Home");


Hope this helps...
Next post: Programatically applying master page in sharepoint

Programatically changing the page layout of default.aspx page MOSS2010

In this post we will change the page layout of default.aspx page MOSS2010 using object modelling.
Below is the code snippet::

        /// <summary>
        /// Method to Change PageLayout For DefalutPage
        /// </summary>
        /// <param name="publishingWeb"></param>
        /// <param name="currentWeb"></param>
        public void ChangePageLayoutForDefalutPage(PublishingWeb publishingWeb, string customPageLayout)
        {

            PublishingPage publishingPage = publishingWeb.GetPublishingPage(publishingWeb.Url + "/Pages/default.aspx");

            if (publishingPage != null)
            {
                List<PageLayout> layouts = new List<PageLayout>(publishingWeb.GetAvailablePageLayouts());
                PageLayout newPageLayout = layouts.Find(
                                delegate(PageLayout l)
                                {
                                    return l.Name.Equals(customPageLayout, StringComparison.CurrentCultureIgnoreCase);
                                });
                if (newPageLayout != null)
                {
                    if (publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
                    {
                        publishingPage.CheckOut();
                    }
                    publishingPage.Layout = newPageLayout;
                    publishingPage.Update();
                    publishingPage.CheckIn("Checked In");
                    publishingPage.ListItem.File.Publish("page published");
                    publishingPage.ListItem.File.Approve("page approved");
                    publishingWeb.Update();

                }

            }


        }

Call the method as below
ChangePageLayoutForDefalutPage(mypublishingwebObj,"myPagelayout.aspx");


Hope this helps..

Programatically applying master page of a sharepoint site MOSS2010

In this post we will see how to Programatically set the master page of a sharepoint site.
And also make sure its subsites inherits the root site master page.
Below is the code snippet::

/// <summary>
        /// method to add master page
        /// </summary>
        /// <param name="CurrentWeb"></param>
        public void AddMasterPage(SPWeb CurrentWeb,string customMasterPage)
        {
            try
            {

                CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4.master";

                CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/" + customMasterPage;

                CurrentWeb.Update();

//To change master page of all subsites
                foreach (SPWeb subweb in CurrentWeb.GetSubwebsForCurrentUser())
                {

                    ChangeMasterPage(subweb, CurrentWeb.MasterUrl, CurrentWeb.CustomMasterUrl);

                }

                CurrentWeb.Update();

            }
            catch (Exception ex)
            {

            }
        }


        /// <summary>
        /// method to change master page
        /// </summary>
        /// <param name="Web"></param>
        /// <param name="pstrMasterURL"></param>
        /// <param name="pstrCustomURL"></param>
        private void ChangeMasterPage(SPWeb subWeb, string pstrMasterURL, string pstrCustomURL)
        {
            try
            {
                subWeb.AllowUnsafeUpdates = true;
                subWeb.MasterUrl = pstrMasterURL;

                subWeb.CustomMasterUrl = pstrCustomURL;

                subWeb.Update();
                subWeb.AllowUnsafeUpdates = false;
                subWeb.Dispose();

            }
            catch (Exception ex)
            {

            }


        }

Call the method as below
AddMasterPage(myweb,"mymasterpage.master");


Hope this helps...
Next Post- Programatically changing pagelayout of default.aspx page in Sharepoint

Thursday, February 16, 2012

Javascript/jquery count li elements inside ul

Below is the script with which you can get the count of li present in ul..


Html::

<ul id="myulid">
  <li>List Item1</li>
  <li>List Item2</li>
  <li>List Item3</li>
  <li>List Item4</li>
  <li>List Item5</li>
</ul>

Java Script:: 


<script type="text/javascript">
var myul = document.getElementById('myulid');
var liCount= myul.getElementsByTagName('li').length;
alert(liCount);
</script>


Hope this helps...

Popular Posts