Monday, October 6, 2014

Deploying Lists and Adding new lists via features

Hi Readers,

This post will give you information on deploying lists , making changes to the lists and adding new lists to the Sharepoint site in O365 or on premise.

What we are using here is feature upgrades, its simple and fast also Sharepoint manages most of the work.

Lets go step by step

First create a project and add a list

Add a new column and name it description



Now from the solution explorer open "Feature1" this will be the feature which we'll be using to deploy and update our lists


 Now press F4 or open the properties pane and give a version to the feature, as its the first deployment I've put version 1.0.0.0

Now package the WSP and upload it the the solution gallery, you can get to solution gallery by going to "Site settings -> Solutions" , and activate the solution.

Now go to site features from site settings and you can view the feature that we have deployed, activate it.



Now our list will be created.


As we have deployed the list, lets see how we can upgrade it, for this I'm going to add a new column to the list,that is called SubTitle

Now open the feature1 again and give it a new version number "2.0.0.0"

Providing a new version number will not update it, we'll have to edit the feature's manifest to do this,
First open feature1 and move to the manifest tab.

 At the bottom you'll see a link saying "Overwrite generated xml file", now you'll see the screen below click the "Edit manifest in the xml editor"

Click that and you'll see the manifest of the feature, the manifest will look like this

Now we'll have to edit the manifest and add upgrade actions to it, here we are asking sharepoint to apply the element manifest if the version is greater than 2.0.0.0.


Now package the WSP.
Here we'll have to do another step before deploying , that is rename the file, then only sharepoint will ask to upgrade it.

Upload the new file and it'll ask you to upgrade the solution. click upgrade.

 Now you can see that the newer version is activated.


 If you go to your list it'll be updated and the new column will be there.


Now we'll see how to add a new library to our feature.
Add a new list and select the type as library


Now you'll see that a new feature is added to your solution, you may delete it, because its not required.


Open the feature again but this time we'll need to go the edit manifest screen and copy the upgrade actions we did in the last part,
Keep it safe in a notepad.

And click the "Discard manifest edits..." and now you can add the new library to the feature using the UI.

Again go to manifest tab and go to XML editor , here we'll have to update the version number and add new upgrade actions, you can see the edits that are done to the xml file. Also the version is increased as well.


You may create the wsp and upgrade the solution but the list will not be created.


If you increase the version again and deploy n upgrade the solution , the list will be created.


You can see that the latest version is activated.

And now your library is created.
You can use this technique to update lists and add new lists, any site that is having this feature will be upgraded and the new lists will be created.

Happy Coding
-Guruparan Giritharan-

Monday, June 2, 2014

Upgradable Sharepoint Webtemplates - Introduction

Hi Readers,

This post is about creating web templates that are upgradable, the method I'm using here is using several features to upgrade the site template.

Let's see the requirement for this, think that your client has a requirement for creating a team site which has a template that is different from the out of the box team site, this may include the contents of the site or even the structure of the site. Your client may ask for a set of lists and libraries that are to be created when a new site is created and the site may have pages that are to be created and webparts are to be placed at those pages.

It may sound simple and you may think that creating a simple site and saving the web template will do the trick, well at some times it will but what if your client comes up with a new library to be added to all the site that are created ?.

Of course you can create a new template after adding the new library, then what happens to all the sites that are already created ?, you may go to each and every site and create the library, what if there are hundreds of sites created ? , will it be practical to go to each site and add the library the answer is No because nobody is ready to do a task like this, You may of course use powershell script or a simple console application to do this but even that may not be possible in production environments.

So lets go to the solution, that is creating a web template and using several features to deploy the content and structure by doing this you can simply update the sites that are created and the site are to be created in the future.

This blog post series will have three more posts
1. Updating lists, adding lists
2. Updating webparts
3. Updating Webpart pages

The concept behind this is Feature Updating and all this is done using sandboxed solutions because it will work in Sharepoint Online as well.
I'm going to create 3 different features and show how to update them, when you create the site you can simply activate them and create a template from it and by upgrading the features you can simply update the sites.

Only problem you may face here is activating a new feature to existing sites, that of course will have to be done using the powershell.

Hope you find this post useful.
-Guruparan-

Sunday, May 11, 2014

Creating a JQuery Plugin

Hi Readers

This post is about creating a plugin using JQuery.
You may have used many plugins that are developed using jquery and wondered how they are developed,

I expect you to know the basics of jqurey to understand this post but will make it as simple as possible.

This post will explain how to create a simple textbox which is a readonly textbox and getting and setting the value will be done via the methods that we create, there is a method to reset the value as well.

Now lets see the coding, all explanations are given as comments.

// Option gets the option settings or the function name
// value is to get the value for setters
$.fn.CustomTextBox = function (option, value) {

    // If options are passed then initialize the textbox
    if (typeof (option) == 'object') {
        //Append the textbox
        //its recommended that you put a class name so that its easy for styling
        $(this).append("<input readonly="" type="text" value="" + option.Text + "" />");
        //Store the default value in the data so that it can be accessed later
        //This method can be used to store the server urls and other setting which needed
        //to be accessed later
        $(this).data("Default", option.Text);
    }
    // If a string is passed its assumed that it a method call
    else {
        //Switch the method based on the string passed
        switch (option) {

            case "Set":
                //If its set then set the value of the textbox
                $(this).find("input").val(value);
                break;
            case "Get":
                //If its get the return the value of the textbox
                return $(this).find("input").val();
                break
            case "Reset":
                //if its reset the get the default value from the data variable and 
                //set it to the textbox
                $(this).find("input").val($(this).data("Default"));
                break;
        }
    }
}

$(document).ready(function () {
    //Initialize the textbox
    $("#container").CustomTextBox({ Text: "Default Text" });

    $("#btnSetValue").click(function () {
        //set the value of the text box
        $("#container").CustomTextBox('Set', "New Value");
    });

    $("#btnGetValue").click(function () {
        //get the value of the text box and alert it
        alert($("#container").CustomTextBox('Get'));
    });

    $("#btnResetValue").click(function () {
        //reset the value of the text box
        $("#container").CustomTextBox('Reset');
    });
});

Now lets see the html section
<div id="container">
</div>
<input type="button" id="btnSetValue" value="Set Value" />
<input type="button" id="btnGetValue" value="Get Value" />
<input type="button" id="btnResetValue" value="Reset Value" />

So thats how you create a simple Jquery plugin.
Happy Coding :)
-Guru-

Friday, April 11, 2014

SQL server Pagination

Hi Readers

This post is about getting sql server results in a paginated manner,

Lets see the requirement
Simply when some one is requesting for a list of data you dont want to retrieve all records from the database and show it to the end user, this will affect the performance of the application and the user will be getting thousands on rows in a single request, think of the network traffic that can make.
So the solution will be getting only a subset of data, that is by pages. Show 1st 10 records of data then show the next 10.

This is easy to do when it you need to show the data that is already ordered by the primary key. you can use it to get the set of records easily, but what if its a dataset that you are showing for a search result, then the order will not be there.

Let see a way to achieve this.
The datasource used for this example is from the Adventure works database.
 We are using the product table in production schema.

Lets take the first 10 rows
ProductID    Name
1        Adjustable Race
2        Bearing Ball
3        BB Ball Bearing
4        Headset Ball Bearings
316    Blade
317    LL Crankarm
318    ML Crankarm
319    HL Crankarm
320    Chainring Bolts
321    Chainring Nut

There are two ways to achieve this
1st way is to use the offset keyword (Available from SQL server 2012)
2nd way is to use the row number and get results

Method 1
Offset - Fetch Method Introduced with SQL Server 2012
This method used the new clause that is offset and fetch
Here we'll have to first use the order by to order the results and fetch the records we need.

SELECT  [ProductID]
      ,[Name]
FROM [AdventureWorks2012].[Production].[Product]
ORDER BY Name
OFFSET N ROWS
FETCH NEXT M ROWS ONLY

This code skips the first 'N' number of rows and returns the next 'M' number of rows.You can use this for pagination by setting the N to the count that you have already read and the items per page as M.

Method 2
Ranking the results
This method simply ranks the results and you can use the rank to get the result set you want using a where clause.

SELECT T.*
FROM
(
    SELECT *,ROW_NUMBER() OVER (ORDER BY NAME DESC) AS [rank]
    FROM [AdventureWorks2012].[Production].[Product]
) AS T
WHERE T.[rank]>((@PageNo*@Count)-@Count) and T.[rank]<(@PageNo*@Count)+1
Here the @PageNo is the number of the page you need details for and the @Count is the number of items per page.

These are simple techniques that i used in one of my projects.

Happy Coding :)
-Guruparan-