Friday, 16 November 2012

jQuery Selectors


jQuery - Selectors

Following table lists down few basic selectors and explains them with examples.
SelectorDescription
NameSelects all elements which match with the given elementName.
#IDSelects a single element which matches with the givenID
.ClassSelects all elements which match with the given Class.
Universal (*)Selects all elements available in a DOM.
Multiple Elements E, F, GSelects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:
  • $('*'): This selector selects all elements in the document.
  • $("p > *"): This selector selects all elements that are children of a paragraph element.
  • $("#specialID"): This selector function gets the element with id="specialID".
  • $(".specialClass"): This selector gets all the elements that have the class ofspecialClass.
  • $("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass".
  • $("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.
  • $("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements.
  • $("ul li:first"): This selector gets only the first <li> element of the <ul>.
  • $("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container.
  • $("li > ul"): Selects all elements matched by <ul> that are children of an element matched by <li>
  • $("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>.
  • $("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>.
  • $("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>.
  • $("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that have a class ofmyclass.
  • $(":empty"): Selects all elements that have no children.
  • $("p:empty"): Selects all elements matched by <p> that have no children.
  • $("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>.
  • $("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass.
  • $("a[@rel]"): Selects all elements matched by <a> that have a rel attribute.
  • $("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname.
  • $("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname.
  • $("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar
  • $("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com.
  • $("li:even"): Selects all elements matched by <li> that have an even index value.
  • $("tr:odd"): Selects all elements matched by <tr> that have an odd index value.
  • $("li:first"): Selects the first <li> element.
  • $("li:last"): Selects the last <li> element.
  • $("li:visible"): Selects all elements matched by <li> that are visible.
  • $("li:hidden"): Selects all elements matched by <li> that are hidden.
  • $(":radio"): Selects all radio buttons in the form.
  • $(":checked"): Selects all checked boxex in the form.
  • $(":input"): Selects only form elements (input, select, textarea, button).
  • $(":text"): Selects only text elements (input[type=text]).
  • $("li:eq(2)"): Selects the third <li> element
  • $("li:eq(4)"): Selects the fifth <li> element
  • $("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other words, the first two <li> elements.
  • $("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements.
  • $("li:gt(1)"): Selects all elements matched by <li> after the second one.
  • $("p:gt(2)"): Selects all elements matched by <p> after the third one.
  • $("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>.
  • $("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>.
  • $("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p>
  • $("li:first-child"): Selects all elements matched by <li> that are the first child of their parent.
  • $("li:last-child"): Selects all elements matched by <li> that are the last child of their parent.
  • $(":parent"): Selects all elements that are the parent of another element, including text.
  • $("li:contains(second)"): Selects all elements matched by <li> that contain the text second.

Wednesday, 17 October 2012

How to print Fibonacci series in c# Using For Loop


How to print Fibonacci series in c# Using For Loop


Code Sample:
using System;
using System.Collections.Generic;
using System.Text;
namespace fibseries
{
class Program
    {
        static void Main(string[] args)
        {
            int num; 
            Console.WriteLine("Please Enter a number:"); 
             num = Convert.ToInt32(Console.ReadLine()); 
             Console.WriteLine("\n"); 
             Console.WriteLine("The first " + num + " number(s) in the fibonacci series are: ");
             FibonacciSeries(num); 
             Console.Read(); 
        }
        // Fibonacci Code
        public static int FibonacciSeries(int n) 
        { 
             int previous = -1; 
             int next = 1; 
             for (int i = 0; i < n; i++) 
             { 
                 int sum = next + previous; 
                 previous = next; 
                 next = sum; 
                 Console.WriteLine(next); 
             } 
             return next;    
         } 
    }

Change View of SharePoint Calendar using javascript/JQuery


Change View of SharePoint Calendar using javascript/JQuery

<script type="text/javascript">
    $(document).ready(function () {
 
        $("#btnMonth").click(function () {
            MoveView('month')
        });
        $("#btnday").click(function () {
            MoveView('day');
        });
        $("#btnweek").click(function () {
            MoveView('week');
        });

    });
   </script>
   <div style="float:right;">
<input id="btnday" value="Day" type="button" />

<input id="btnweek" value="Week" type="button" />

<input id="btnMonth"  value="Month" type="button" />
</div>

Tuesday, 16 October 2012

c# program to print patterns of numbers and stars(pyramid)


These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
    *
   ***
  *****
 *******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

C programming code

class Program
    {
        static void Main(string[] args)
        {
            int row,c,n,temp;
            n = 5;
           temp= n ;
           for (row = 1; row <= n; row++)
           {
               for (c = 1; c < temp; c++)
                   Console.Write(" ");
               
               temp--;
               for (c = 1; c <= 2 * row - 1; c++)
                   Console.Write("*");

               Console.WriteLine();
           
           }


            
        }
    }

Tuesday, 18 September 2012

How to reset all validators and validation summary on asp.net page

How to reset all validators and validation summary on asp.net page 


<script type="text/javascript">
    function Page_ClientValidateReset() {
        if (typeof (Page_Validators) != "undefined") {
            for (var i = 0; i < Page_Validators.length; i++) {
                var validator = Page_Validators[i]; 
                validator.isvalid = true;
                ValidatorUpdateDisplay(validator);
            }
        }
 $("[id$=Validationsummary]").html("");  
    }
</script>
 
 Click to view detail

Thursday, 13 September 2012

LINQ TO SHAREPOINT: WORKING WITH CREATED, CREATEDBY, MODIFIED AND MODIFIEDBY








LINQ to SharePoint is a great tool to perform queries against a SharePoint server since the 2010 version
Unlike the classical CAML queries, it allows to use a strongly-typed entity model and LINQ query syntax to
 query list data.


The SPMetal command
The first step to use LINQ to SharePoint is to run the SPMetal tool in order to create the entity model from an
 existent SharePoint site. This tool is located at 14\bin. Here’s a sample on how to use it:
SPMetal /web:http://mysharepointsite:9999 /code:Model.cs
This command will create a C# code file containing the entity model, in 14\bin\Model.cs. After adding this file
to our project, we can perform queries using LINQ to SharePoint. For example, this server-side code, outputs the
 titles for all the items in “MyList” where the title length is at least 10 characters long:
StringBuilder output = new StringBuilder();
using (ModelDataContext model = new ModelDataContext(SPContext.Current.Site.Url))
{
    foreach (MyListItem itemWithoutTitle in model.MyList.Where(x => x.Title.Length >= 10))
    {
        output.AppendLine(itemWithoutTitle.Title);
    }
}
The missing fields
By default, the Created, CreatedBy, Modified and ModifiedBy fields are not created by SPMetal. However,
the framework offers a way of extending the object-relational mapping system of the LINQ to SharePoint provider.
In other words, we can easily use those fields after telling LINQ to SharePoint how to retrieve and update them from
 the content database.
We will extend the base entity class of our model (“Item” class) in a new code file (we can call it “ModelExtensions.cs”
 for example):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
public partial class Item : ICustomMapping
{
    [CustomMapping(Columns = new String[] { "Modified", "Created", "Editor"
"Author" })]
   public void MapFrom(object listItem)
    {
        SPListItem item = (SPListItem)listItem;
        this.Modified = (DateTime)item["Modified"];
        this.Created = (DateTime)item["Created"];
        this.CreatedBy = (string)item["Author"];
        this.ModifiedBy = (string)item["Editor"];
    }
    public void MapTo(object listItem)
    {
        SPListItem item = (SPListItem)listItem;
        item["Modified"] = this.Modified;
        item["Created"] = this.Created;
        item["Author"] = this.CreatedBy;
        item["Editor"] = this.ModifiedBy;
    }
    public void Resolve(RefreshMode mode, object originalListItem, object databaseObject)
    {
        SPListItem originalItem = (SPListItem)originalListItem;
        SPListItem databaseItem = (SPListItem)databaseObject;
        DateTime originalModifiedValue = (DateTime)originalItem["Modified"];
        DateTime dbModifiedValue = (DateTime)databaseItem["Modified"];
        DateTime originalCreatedValue = (DateTime)originalItem["Created"];
        DateTime dbCreatedValue = (DateTime)databaseItem["Created"];
        string originalCreatedByValue = (string)originalItem["Author"];
        string dbCreatedByValue = (string)databaseItem["Author"];
        string originalModifiedByValue = (string)originalItem["Editor"];
        string dbModifiedByValue = (string)databaseItem["Editor"];
        if (mode == RefreshMode.OverwriteCurrentValues)
        {
            this.Modified = dbModifiedValue;
            this.Created = dbCreatedValue;
            this.CreatedBy = dbCreatedByValue;
            this.ModifiedBy = dbModifiedByValue;
        }
        else if (mode == RefreshMode.KeepCurrentValues)
        {
            databaseItem["Modified"] = this.Modified;
            databaseItem["Created"] = this.Created;
            databaseItem["Author"] = this.CreatedBy;
            databaseItem["Editor"] = this.ModifiedBy;
        }
        else if (mode == RefreshMode.KeepChanges)
        {
            if (this.Modified != originalModifiedValue)
            {
                databaseItem["Modified"] = this.Modified;
            }
            else if (this.Modified == originalModifiedValue && this.Modified != dbModifiedValue)
            {
                this.Modified = dbModifiedValue;
            }
            if (this.Created != originalCreatedValue)
            {
                databaseItem["Created"] = this.Created;
            }
            else if (this.Created == originalCreatedValue && this.Created != dbCreatedValue)
            {
                this.Created = dbCreatedValue;
            }
            if (this.CreatedBy != originalCreatedByValue)
            {
                databaseItem["Author"] = this.CreatedBy;
            }
            else if (this.CreatedBy == originalCreatedByValue && this.CreatedBy != dbCreatedByValue)
            {
                this.CreatedBy = dbCreatedByValue;
            }
            if (this.ModifiedBy != originalModifiedByValue)
            {
                databaseItem["Editor"] = this.ModifiedBy;
            }
            else if (this.ModifiedBy == originalModifiedByValue && this.ModifiedBy != dbModifiedByValue)
            {
                this.ModifiedBy = dbModifiedByValue;
            }
        }
    }
    public DateTime Modified { get; set; }
    public DateTime Created { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
}
For extended information of how the ICustomMapping interface works, you can check these MSDN articles: ICustomMapping Members and RefreshMode Enumeration.
After adding this file to our project, we can use Modified, Created, CreatedBy and ModifiedBy in our queries:
StringBuilder output = new StringBuilder();
using (ModelDataContext model = new ModelDataContext(SPContext.Current.Site.Url))
{
    DateTime date = DateTime.Parse(“Thu, 05 May 2011 12:46:00 GMT”);
    foreach (MyListItem itemCreatedAfterDate in model.MyList.Where(x => x.Created > date))
    {
        output.AppendLine(itemCreatedAfterDate.Title);
    }
}
Keep in mind that the Author and Editor fields identify users. These strings may have more information than what
 you need. An easy way of parsing this string to extract the information you need is to create a new SPFieldUserValue
with the current SPWeb and the string. Then you can extract the actual SPUser from SPFieldUserValue.User.
I hope you find this code useful as I do. Enjoy!

Sunday, 9 September 2012

Adding functionalities to pages by inheriting PublishingLayoutPage


If you have worked a lot with MOSS you probably know how to make new page layouts. But if you create new page layouts you might sometimes wonder that how could I add some common functionalities to my page layout pages. One example could be localization. You have decided that Variations isn't the way to go in your case, but you still want to have different site structures for different languages... and of course you want to have texts localized. Or you want to change your master page for some reason on the fly... one example could be for printing reasons. Or even wilder... you want to change you page layout to another! You could do this kind of stuff pretty easily if you create your own PublishingLayoutPage class that has support your new functionalities. I'm going to explain how you can do that with SharePoint Designer and Visual Studio.

Create new class that will extend the functionality of PublishingLayoutPage

I started my journey by creating new Class Library project. I named it "Microsoft.MCS.Common" (since I work in MCS inside Microsoft... cool naming right :-). I added new class and named it PublishingLayoutPageEx. I inherited that from PublishingLayoutPage which is class behind page layouts. Where did I got that class name? Well I just opened ArticleLeft.aspx with SharePoint Designer and checked the first line:

<%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage, Microsoft.SharePoint.Publishing, Version=12.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
So it was pretty obvious that if I want to extend the functionality of the basic publishing page, I needed to inherit from it.
At this point my code looked like this (not much since we just started):
 1  using System;
 2  using System.Collections.Generic;
 3  using System.Text;
 4  using System.Web.UI;
 5  using System.Globalization;
 6  using System.Threading;
 7  using Microsoft.SharePoint.Publishing;
 8  using Microsoft.SharePoint;
 9
10  namespace Microsoft.MCS.Common
11  {
12    public class PublishingLayoutPageEx : PublishingLayoutPage
13    {
14      public PublishingLayoutPageEx()
15        : base()
16      {
17      }
18    }
19  }

And now I'm ready to test my new class in action. I just added strong name key, compiled and put it in the GAC. And then I changed the ArticleLeft.aspx to use my new class:
<%@ Page language="C#" Inherits="Microsoft.MCS.Common.PublishingLayoutPageEx, Microsoft.MCS.Common,Version=1.0.0.0, Culture=neutral,PublicKeyToken=b1e9400215c03709" %>
<small sidetrack to .NET Reflector>
If you're interestested in the stuff that's implemented in PublishingLayoutPage, then you can play around with incredible tool: Lutz Roeder's NET Reflector:
In just few clicks we can see that there is some MasterPageFile retrieving in OnPreInit:

</small sidetrack to .NET Reflector>

If you now try your new PublishingLayoutPageEx in action you'll get this kind of error message:

Server Error in '/' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The base type 'Microsoft.MCS.Common.PublishingLayoutPageEx' is not allowed for this page. The type is not registered as safe.

Source Error:


 <%@ Page language="C#"   Inherits="Microsoft.MCS.Common.PublishingLayoutPageEx,Microsoft.MCS.Common,Version=1.0.0.0,Culture=neutral,PublicKeyToken=b1e9400215c03709" %>
 <%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">

Source File: /_catalogs/masterpage/ArticleLeft.aspx    Line: 1


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

That only means that we need to mark that component as Safe so that SharePoint will load it. Let's just modify our applications web.config file by adding following line in there:
<SafeControl Assembly="Microsoft.MCS.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b1e9400215c03709" Namespace="Microsoft.MCS.Common" TypeName="PublishingLayoutPageEx" Safe="True" AllowRemoteDesigner="true" />
And then hit F5 in your browser and you should be all set. Now you have base what we're going to extend in next.

Add localization support to your pages

If you haven't played with ASP.NET Resource files, then you should take small detour into www.asp.net localization quickstart.
So now you know about .RESX files :-) I created Example.resx, Example.en-US.resx and Example.fi-FI. I have added only two words to the resource files:
  1. House:
    • en-US: House
    • fi-FI: Talo
  2. You:
    • en-US: You
    • fi-FI: Sinä
I copied those resource files to my applications App_GlobalResouces folder:
C:\Inetpub\wwwroot\wss\VirtualDirectories\80\App_GlobalResources
Now I modified my default_Janne.master page so that it would receive text from my resource files. I added following line just before </body> in master page.
<asp:Literal ID="house" runat="server" Text="<%$Resources:Example,House%>" /> <-> <asp:Literal ID="you" runat="server" Text="<%$Resources:Example,You%>" />
We have now added resource files and modified master page so that it will take text from our resource file. Let's just add code to our new class so that we could change the language on the fly.
 1  using System;
 2  using System.Collections.Generic;
 3  using System.Text;
 4  using System.Web.UI;
 5  using System.Globalization;
 6  using System.Threading;
 7  using Microsoft.SharePoint.Publishing;
 8  using Microsoft.SharePoint;
 9
10  namespace Microsoft.MCS.Common
11  {
12    public class PublishingLayoutPageEx : PublishingLayoutPage
13    {
14      public PublishingLayoutPageEx()
15        : base()
16      {
17      }
18
19      protected override void OnPreInit()
20      {
21         base.OnPreInit();
22         this.InitializeCulture();
23      }
24
25      protected override void InitializeCulture()
26      {
27         if (Request["mylang"] != null)
28         {
29           Thread.CurrentThread.CurrentCulture = new CultureInfo(Request"mylang"].ToString());
30           Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request["mylang"].ToString());
31         }
32
33         base.InitializeCulture();
34      }
35    }
36  }

And now we can change the language from URL:

Here is result without the mylang parameter:

Of course you might not want to change your language by url parameter :-) This is just sample that you CAN do that. Maybe it would be much wiser to use some kind of site structure for localization. But I'll leave that to you...

Change master page on the fly

Now we want to make something fancier... like changing the master page on the fly. You could want to use this for print layouts, smaller screen, mobile etc. But anyway.. You just might want to do that sometimes :-)
So let's throw some code in here and see what happens:
...
 1      protected override void OnPreInit()
 2      {
 3         base.OnPreInit();
 4         if (Request["Print"] != null)
 5         {
 6           this.MasterPageFile = "/_catalogs/masterpage/BlueBand.master";
 7         }
 8      }
... 
On lines 4 to 6 we have just check that if there is mysterious Print parameter set. If that is set, we'll change the master page to nicely hardcode one. Let's see what happens on our browser:

So the result is quite easy to see... our master page changed from default_janne.master to BlueBand.master.

Change the page layout

Before I start... I'm going to give credit of this idea to Vesa Juvonen (colleague of mine at MCS who also works with SharePoint). He said that this would be interesting thing to checkout. And since I happened to have some code ready we tried this stuff on my environment. But he's going to create full solution of this page layout change and publish it in his blog. So you probably want to check that place out too. Okay.. but let's get back to the subject.
This might sound a bit strange but still... sometimes you might want to change page layout after the page has been created. Consider the Article Page content type which is OOB content type in SharePoint. It has 4 different kind of page layouts. User could have selected Image on right layout and has filled the page with data. After a while you want to change it to another layout.... BUT there isn't easy way to do that... unless we'll extend our nice class again.
Idea is take (again) some nice url parameter that tells the destination page layout. In this example I'll just take integer which is used to get the correct page layout from array of possible page layouts of this content type. And yes... I know that this code sample has a lot to improve... It just gives you ideas.
Let's throw some code in here and see what happens:
...
 1      protected override void OnPreInit()
 2      {
 3        SPContext current = SPContext.Current;
 4        if (current != null &&
 5            Request["changepagelayout"] != null &&
 6            Request["done"] == null)
 7        {
 8          SPWeb web = current.Web;
 9          // We need to allow unsafe updates in order to do this: 10          web.AllowUnsafeUpdates = true;
11          web.Update();
12       
13          PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
14          PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(current.ListItem.ContentType.Parent.Id);
15          PublishingPage publishingPage = PublishingPage.GetPublishingPage(current.ListItem);
16          publishingPage.CheckOut();
17          // This is the magic:18          publishingPage.Layout = layouts[Convert.ToInt32(Request["changepagelayout"])];
19          publishingPage.Update();
20          publishingPage.CheckIn("We have changed page layout");
21       
22          SPFile file = current.ListItem.File;
23          file.Publish("Publishing after page layout change");
24          // We have content approval on: 25          file.Approve("Approving the page layout change");
26          Response.Redirect(Request.Url + "&done=true");
27        }
28        base.OnPreInit(e);
29      }
... 
And you can right away see from code that there isn't any checks or any error handling. So this code is only for demonstration purposes and you shouldn't take it any other way.... but here we can see the results after user has type in parameter changepagelayout=1 (Image on left):

And here is page if parameter is 2 (Image on right).

If you look at the code on line 14 where page layouts are retrieved... I'm using Parent of the current content type. You might ask why... But the reason is simple since your content type from the list actually inherits the Article Page content type from Site collection level. So if you would use ListItem.ContentType you wouldn't get those 4 page layouts of Article Page. Insted you need to get the parent of the content type in the list and then you get 4 different page layouts. Makes sense if you think how inheritance in SharePoint works.
If you wonder that done parameter I'm using... It is just helper to avoid recursive page layout change ;-)
Note: If you look at the code you probably already noticed that it's not changing the layout for this rendering... it has changed the page layout permanently. Of course you can change it back if you want to.

Summary

You can use a lot of stuff from ASP.NET right in your SharePoint page layouts. I can't even imagine all the capabilities of this but I'm just going to give you brief list what I can think of now:
1) Localization:
  • This one is obvious :-) I live in Finland and we need to deal with this in every project.
2) Change the master page
  • Print layouts
  • Mobile UIs
3) Change page layout
  • If you had created page but you later on want to change to more suitable one... here's how you can do it

I hope you got the idea of this post. I know I could improve those samples a lot, but I just wanted to share my idea and give you the opportunity to make it much better than I did.

Anyways... happy hacking!