Monday, November 19, 2012

Simple jQuery Tabbing with Time interval

Hi Friends,

I thought to share simple things more frequently and occasionally post more detail findings (since we are on war to fight with time :) )

If your div structure is below:

<body onload="javascript:TabInterval();">

<div id="div1">
<p>This is Div 1</p>
</div>
<div id="div2" style="display: none;">
<p>This is Div 2</p>
</div>
<div id="div3" style="display: none;">
<p>This is Div 3</p>
</div>

</body>

We can simply rotate these divs using .show() and .hide() methods

So, Try with below jQuery script

<script type="text/javascript">
//starting point for the rotation, after initially load div 1  
var startRootDiv = 2;
    function rotateAllDivs() {
        if (startRootDiv == 2) {
            $("#tab1").hide();
            $("#tab2").show();
            startRootDiv = 3;
        }
        else if (startRootDiv  == 3) {

            $("#tab2").hide();
            $("#tab3").show();           

            startRootDiv = 1;
        }
        else {

            $("#tab1").show();
            $("#tab3").hide();           

            startRootDiv = 2;
        }
    }
    function TabInterval() {
        setInterval(rotateAllDivs, 5000);
    };
</script>

This div tabbing configured to run with a time interval of 5 seconds.

Happy simple coding :)

Buddike

Wednesday, November 7, 2012

Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

Hi Guys,

You may have faced this kind of error while coding the webparts and provisioning them in SharePoint pages. May be you will face this issue while trying to edit a page, that having the webpart with below code

using(SPWeb web = SPContext.Current.Web)
{
//Your code here
}

Issue is with using statement, its disposing your SPWeb object automatically when you leaving using block. Use below than the above block

SPWeb web = SPContext.Current.Web

Hope you will figure it out within your code.

Happy coding guys,

Buddike

Thursday, November 1, 2012

SharePoint Search Refinement Panel

Hi Guys,

You may like to learn more about search refinement panel and how to use it.

Scenario:

Think that your client have several departments and each department having seperate documents to be use. Each department is a site collection and these specific documents residing in a document library called "Department Docs". So client asking for search refinement to implement.

My steps will be like below:

1.Create a Site column for holding departments, type : choices
2.Check the Document library residing in side the department site collection
3.Add the column to the default view of the document library
4.Add several documents to the library
5.Do a incremental crawl to the content source, through Central Admin-> Application Management -> Search Application-> Content Source
6.Wait till crawl completes
7.Create Managed Meta Data property
8.Do a full crawl
9.Create a Enterprise Search Centre page
10.Modify the page and Edit Refinement Panel webpart
11. Modify below category markup and save the webpart

<Category Title="Departments" Description="Departments docs" Type="Microsoft.Office.Server.Search.WebControls.ManagedPropertyFilterGenerator" MetadataThreshold="0" NumberOfFiltersToDisplay="3" MaxNumberOfFilters="20" ShowMoreLink="True" MappedProperty="Departments" MoreLinkText="show more" LessLinkText="show fewer" />


Note:
Title: appear in the refinement panel
MetadataThreshhold : if your department column having metadata this is the minimum value to use when displaying the results
MappedProperty : This is the Field name you have used to create the managed metadata.
12.Checkin the page
13.Run a search...There you can find the refinement values you have just configured.

I'll update this post with more screens, while time permits

Happy coding,

Buddike

Tuesday, October 30, 2012

Debugging a Timer job

Hi Friends,

May be you are running with the time for creating Sharepoint timer job creation. You have started to create a feature with Event receivers, feature receivers and basic timer job class structure like below.

public class TimerJobClass : SPJobDefinition
{
      //Your contructors to handle different combinations of parameters
      public TimerJobClass()
     {
     }
    
     public TimerJobClass(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
     {
     }

     public TimerJobClass(string jobName, SPWebApplication webApplication, string targeturl)
            : base(jobName, webApplication, null, SPJobLockType.Job)
        {
            this.Title = "Timer Job Name";
        }
    
      public override void Execute(Guid contentDbId)
      {
            //Things need to do.
      }
}

After this structure declaring, you may be started with sample method designing and try to debug the timer job with below line

System.Diagnostics.Debugger.Break();

You may be wonder that you have copied the dll to GAC and did a IISRESET, and ran the sp timer job definition but the debugging engine of the VS 2010 never fires even after run the timer job.

I know your problem, Restart the "Sharepoint 2010 Timer" job  and then start the IISRESET. ....magic appears.....there you get the debugger fires upon the timer running.

Happy coding,

Buddike


Thursday, September 27, 2012

ECMA scripting

ECMA Scripting and WebParts in Sharepoint.

Hey,

Today I'm planning to present "How to do a webpart using client object model, which is capable of replicating multiple time within a same page". Most of Sharepoint developers developing webparts using client object model with ECMA scripting. However duplicate webparts cant included in a same page.

But below is a nice approach on solving above issue.

1.Create a visual webpart which is inheriting from System.Web.UI.WebControls.WebParts.WebPart
2.In the .cs file just add hidden property with webpart's clienID and
3.Read that client id to commit any changes.

I have attached a sample project consisting this solution. Open the solution from VS 2010.

File structure:




BlogViewer.cs - holding one webpart property, saving the property value to a hidden field, registering the js file, register main function of js - which is having the webpart client id to refer the control/s.

comscript.js - get the webpart client id, access webpart property value - which is already write to the hidden field, then populate the html file inside the webpart div.

control.htm - holding the markup of the webpart

Download from here : ECMAScripting


Monday, March 12, 2012

Debugging Email enabled list functionality

Hi Folks,

Sure you will be running out of your gas while searching whats happening to your debugging session, while receiving the emails to your Email enabled list instance (Ex: Announcement list). Do you have face below scenario ->

1.You create a Email enabled list instance
2.Create a event receiver to process incomming email objects.
3.Attach Debugger process or enter System.Diagnostics.Debugger.Break() above your lines
4.Rebuild and Deploy the solution (did a IISRESET )
5.List instance appearing and you have configured email address
6.Send sample email.
7.Email delivered to Drop folder, after sometime email object just vanished but debugger session not started.

If this is the case, no worries :). Just restart the Sharepoint timer job service through services. If you having VS 2010 installed there is a cryptographic exception related session try to start, just cancel it and restart the service again. Then after try to send a sample email to the list instance, you will get the debugging session and VS direct you to the EmailReceived() method or any other method

Sunday, February 26, 2012

Sharepoint 2010 Variations and Culture base subsite navigation

I'm planning to discuss sharepoint 2010 variations base features in this article. We can use variations to filter content according to the user browser language, and show. Just follow below steps and see how easy it is.

Pre-Requisittes:

  • Create Web Application and a "publishing portal" site collection.
Steps to follow:

  1. Access "Site Action" -> "Site Settings"

2. See the "Site collection administration" section (check below screenshot)




3. Click on "Variations" (in here you can configure variation root site settings and other settings






4. Click on "Variation labels" from the view mentioned in step2







5. Repeat the above 4th step for "fr-FR" label



6. Click "Create Hierarchies" link from "Variations" screen mentioned in Step 4



7. Run the Timer job from Central administration






8. Open new browser and check the Language settings as in screens below.







9. Change the browser language and restart the browser. You will notice that fr-FR related content loading




when language changed to fr-FR and can observe the same behaviour for en-US. Enjoy variations stuff!

Tuesday, January 17, 2012

Data View Webpart Show/Hide Accidently

Hey folks, yesterday I have faced a weird issue, it's in a webpart that I have designed. Result is a high level defect :)

Issue: Dataview webpart not showing the list contents properly, it's just vanished. Development environment it's properly showing.

So I have started to search the root cause of the issue

1.Checked whether list instance properly deploying.....passed
2.Check dataview webpart markup in onet.xml and verify deployment......passed
3.Check and compare the xslt and parameters passing to the webpart......passed
4.Check Styles of the webpart...........issue occured

I have noticed webpart zone having a inline style as "display:none" which is hiding the webpart. So I have gone through the webpart styles again and unfortunately didn't find any inline style, causing issue.

So I have edit page:

1.Click on Restore webpart
2.Publish the page

Issue solved successfully.

Learning points: If we set restore on webpart it's get hide by adding inline style as "display:none". However this can be helpful to you in your daytoday life, so decided to share.

Monday, January 2, 2012

Javascript Water mark effect for your password field

Most of web developers like or not, they have to deal with forms every time. There can be password fields available in your form itself with a magical effect of water mark. Herewith I would like to simulate a way to implement this functionality.

Functionality: We can have 3 textboxes (one to hold password, second to hold default text to be appeared and last one for move between 2 text boxes previously mentioned). So if there were no text in first text box we show second text box with default text, if else show password text with letters with hidden characters. 

So lets strart to implement.

1.Open VS 2008/2010 and open a new ASP.Net project
2.Add Text box and make it a password type (see the code snippet below)

<div id="divPassword">
        <asp:TextBox ID="txtCurrentPassword" TextMode="Password" runat="server"
            onblur="restorePassword('divPassword', 'divDummyPassword')"></asp:TextBox>
    </div>

Note: javascript function refer in onblur attribute will be describe in below steps

3.Then add next text box which is holding the default text to be appeared when there were no characters

<div id="divDummyPassword">
        <asp:TextBox ID='txtDummyPassword' runat='server' Text="Enter your current password"
            onfocus="changePassword('divPassword', 'divDummyPassword')"></asp:TextBox>
    </div>

4.Then add 3rd text box which is a hidden text box to show when user click on first text box 

<div>
        <input id="PasswordHidden" runat="server" type="hidden" />
    </div>

As you can see we are calling "onblur" function and "onfocus" function in different occasion. It's because we are going to load default text based text box initially with "onfocus" and "onblur" used while leaving password text box.

So lets see how our 2 javascripts being design

<script language="JavaScript" type="text/javascript">
        function changePassword(divPassword, divDummyPassword) {
            document.getElementById(divPassword).style.display = '';
            document.getElementById(divDummyPassword).style.display = 'none';
            document.getElementById('<% =txtCurrentPassword.ClientID %>').focus();
        }

        function restorePassword(divPassword, divDummyPassword) {
            if (document.getElementById('<% =txtCurrentPassword.ClientID %>').value == '') {
                document.getElementById(divPassword).style.display = 'none';
                document.getElementById(divDummyPassword).style.display = '';
            }
        }
    </script>

As you can see, we just show and hide 2 divs, clear and assign focus appropriately to view and show the default text. So what about hidden field we implemented. Whenever user click on default text, our hidden textbox holding the values we are entering with temporary basis and then assign our values to correct text box for use further.

<script language="JavaScript" type="text/javascript">

        if (document.getElementById('<% =PasswordHidden.ClientID %>').value != '' && document.getElementById('<% =txtCurrentPassword.ClientID %>').value == '') {
            document.getElementById('<% =txtCurrentPassword.ClientID %>').value = document.getElementById('<% =PasswordHidden.ClientID %>').value;
            document.getElementById('divPassword').style.display = '';
            document.getElementById('divDummyPassword').style.display = 'none';
        }
        else {
            document.getElementById('divPassword').style.display = 'none';
            document.getElementById('divDummyPassword').style.display = '';
        }
    </script>

Note: this script should appear just above the end of form tag.

Whole sample:

<form runat="server">



 <script language="JavaScript" type="text/javascript">




        function changePassword(divPassword, divDummyPassword) {
            document.getElementById(divPassword).style.display = '';
            document.getElementById(divDummyPassword).style.display = 'none';
            document.getElementById('<% =txtCurrentPassword.ClientID %>').focus();
        }


        function restorePassword(divPassword, divDummyPassword) {
            if (document.getElementById('<% =txtCurrentPassword.ClientID %>').value == '') {
                document.getElementById(divPassword).style.display = 'none';
                document.getElementById(divDummyPassword).style.display = '';
            }
        }
    </script>


    <div id="divPassword">
        <asp:TextBox ID="txtCurrentPassword" TextMode="Password" runat="server"
            onblur="restorePassword('divPassword', 'divDummyPassword')"></asp:TextBox>
    </div>
    <div id="divDummyPassword">
        <asp:TextBox CssClass="txt_w187" ID='txtDummyPassword' runat='server' Text="Enter your current password"
            onfocus="changePassword('divPassword', 'divDummyPassword')"></asp:TextBox>
    </div>
    <div>
        <input id="PasswordHidden" runat="server" type="hidden" />
    </div>


    <script language="JavaScript" type="text/javascript">


        if (document.getElementById('<% =PasswordHidden.ClientID %>').value != '' && document.getElementById('<% =txtCurrentPassword.ClientID %>').value == '') {
            document.getElementById('<% =txtCurrentPassword.ClientID %>').value = document.getElementById('<% =PasswordHidden.ClientID %>').value;
            document.getElementById('divPassword').style.display = '';
            document.getElementById('divDummyPassword').style.display = 'none';
        }
        else {
            document.getElementById('divPassword').style.display = 'none';
            document.getElementById('divDummyPassword').style.display = '';
        }
    </script>


    </form>


Note: Don't just copy and paste, allocate some time to understand the flow of the script

Get ruleset violation using FxCop

Most of you may wondering, how to analyse and find whether your VS 2008 project having RuleSet violations. You can install FxCop and check whether your compiled project having RuleSet violations.

You can read more about FxCop in below url:

msdn url for FxCop

Steps to follow:

1.Build your VS project using "Release" mode



2.Open Installed FxCop and open new project



3.Set appropriate rules to be use when analysis running




4.Attach your exe in bin folder to "Add target for analysis"



5.Run analysis

6.View results and apply fixes