Pages

Tuesday 27 March 2012

The Best Way to Add Custom JavaScript and jQuery to SharePoint

During extensive SharePoint user interface customization you'll likely encounter a scenario where you need to make a web part or user control do something it was not intended to do or have a look that cannot be accomplished using the CSS hooks provided out-of-the-box. The solution is to create a custom master page and include a reference to a JavaScript file where you can modify the Document object. While straight JavaScript will do, I prefer to use the jQuery JavaScript library, which is far more robust, easier to use, and allows for plugins. Follow the steps below to add jQuery to your master page.
       
Go to jquery.com and download the latest jQuery library to your desktop. You want to get the compressed production version, not the development version.
Open SharePoint Designer (SPD) and connect to the root level of your site's site collection.
In SPD, open the "Style Library" folder.
Create a folder named "Scripts" inside of the Style Library.
Drag the jQuery library JavaScript file from your desktop into the Scripts folder.
In the Scripts folder, create a new JavaScript file and name it (e.g. "actions.js").
Open your master page file in SPD.
Within the <head> tag of the master page, add a script reference to the jQuery library just above the content place holder named "PlaceHolderAdditonalPageHead" (and above your custom CSS references, if applicable) as follows:
<script src="/Style%20Library/Scripts/{jquery library file}.js" type="text/javascript"></script>
Immediately after the jQuery library reference add a script reference to your custom scripts file as follows:
<script src="/Style%20Library/Scripts/actions.js" type="text/javascript"></script>
       
 
Your custom master page now includes jQuery and a reference to your custom scripts file where you can add jQuery scripts. SharePoint includes a number of JavaScript files throughout the site, so be careful that the scripts you add do not conflict with SharePoint's; the jQuery library itself does not conflict with SharePoint.

The Best Way to Add Custom CSS to SharePoint

To thoroughly customize your SharePoint site, you'll need to use a custom CSS. SharePoint offers a way to specify a single CSS file to use via the "Master page" settings for the site. However, using this approach still limits you to customizing only those IDs and classes that are included on the pages by SharePoint. Moreover, using this setting applies to both site and system pages, which may not be desirable. With these downsides in mind,Follow the steps below to include a custom CSS file in your master page.
       

1. Open SharePoint Designer (SPD) and connect to the root level of your site's site collection.
2. In SPD, open the "Style Library" folder.
3. Create a new CSS file and name it (e.g. "customstyles.css").
4. Open your master page file in SPD.
5. In the  tag add a link to your custom CSS just above the content place holder named "PlaceHolderAdditonalPageHead" as follows:
<link href="/Style%20Library/customstyles.css" rel="stylesheet" type="text/css" />

       
 
Your custom master page is now using your new CSS file in addition to all of the CSS files SharePoint uses out-of-the-box. The idea is that you are taking advantage of the "cascading" property of cascading style sheets by layering your custom styles on top of what SharePoint creates in order to alter the SharePoint look-and-feel as desired—like a skin.

How to Create a Custom SharePoint Master Page

The first step in customizing your SharePoint site is to create a custom master page. The following steps will help you do just that (please note there are differences indicated between SP 2007 and SP 2010).
       

1. Open SharePoint Designer (SPD) and connect to the root level of your site's site collection.
2. In SPD, open the "_catalogs" folder, then open the "masterpage" folder.
3. Identify the out-of-the-box (OOTB) most like your ultimate design.
    i)(SP 2007) If your SharePoint's site design is to be fixed-width and centered on the page, select BlueBand.master and copy it.
   ii)(SP 2007) If your SharePoint's site design is to be a liquid layout that fills the page regardless of the user's screen resolution, select default.master and copy it.
  iii)(SP 2010 beta) You want v4.master.
4. In SPD, right-click on the master page you want to duplicate and select "Copy," then paste it back into the same folder.
5. Rename the new file something project-specific.  For instance, if this master page is to be used on the sales portal, you might rename the new master page SalesPortal.master.
6. Publish and approve the new master page.
7. In your browser, navigate to your site's Site Settings page.  Under the "Look-and-Feel" column click "Master page."
8. Select your new master page as the site master page (the need to set the system master page will vary by project).  Save the settings change.
   
Your site is now using the new master page you created. From here you modify the master page to your liking, including adding custom CSS and custom JavaScript and jQuery.

Covariance and Contra variance in .Net 4.0 Framework

Covariance and Contra variance in Delegates: There has always been confusion for me to understand the concept of Covariance and Contra variance. And today when I scaled down to this definition and example to understand them, it has become really easy to understand it. So here I am sharing with you all. Covariance: In earlier versions of Visual C#, a delegate method must be exactly matched to a delegate signature. To create a successful match, the parameter type and the return type of the delegate method must be identical to the parameter type and the return type of the delegate signature. When we talk about covariance in .net, we say it permits a method to have a more derived return type than what is defined in the delegate. In Visual C# 2005, you may notice that more than one match can be established when you work in an inheritance context. In this scenario, delegate behavior may change.

When you compile and run this code sample in the Microsoft .NET Framework 2.0, you receive a result of "String". In the .NET Framework 2.0, both the M method in the base class and the M method in the derived class can be matched to the delegate signature. However, overload resolution rules decide that the one method in the most specific class wins the match. In this example, the method that is in class B wins the match. Covariant type parameters enable you to make assignments that look much like ordinary polymorphism. Polymorphism enables you to assign an instance of derived class to a variable of type base class. Similarly, because the type parameter of the IEnumerable interface is covariant, you can assign an instance of IEnumerable to a variable of type IEnumerable, as shown in the following code.

This gave an error “Cannot implicitly convert type” in Framework 2.0. But it is compiles without error in Framework 4.0. The List class implements the IEnumerable interface, so List implements IEnumerable. The covariant type parameter does the rest. Contra variance: When a delegate method signature has one or more parameters of types that are derived from the types of the method parameters, that method is said to be contra variant. As the delegate method signature parameters are more specific than the method parameters, they can be implicitly converted when passed to the handler method. Contra variance therefore makes it easier to create more general delegate methods that can be used with a larger number of classes

Now have you ever imagine that IComparer and IComparer are one and the same. It is possible in Framework4.0 because the interface IComparer has become IComparer. The “in” indicates that the parameters are restricted to occur only in input positions. Let’s go for an example to explain the contra variance in Framework4.0.

To weigh animals, we will create WeightComparer which will implement IComparer interface; which in turn will enable us to use the Sort() method on list object.

Now we will check by adding making a list of animals and sorting it and making a list of elephants and sorting it using the Sort() method of IComparer.

In the above code prior to .Net 4.0 Elephants list could not have been sorted it gives error “Cannot convert from 'WeightComparer' to 'System.Collections.Generic.IComparer”. Above samples have cleared the concepts of covariance and contra variance; also have explained the enhancement in these concepts in .Net Framework 4.0.

Error occurred in deployment step 'Add Solution': Property 'SiteUrl' contains an invalid URL

While deploying custom BDC from visual studio, we may receive error "Error occurred in deployment step 'Add Solution': Property 'SiteUrl' contains an invalid URL".

Workaround:
1. Edit feature template file say Feature1.Template.xml
2. add following property under <properties> element
<Property Key='SiteUrl' Value='http://your_site_url'/>

Popular Posts