Monday, July 12, 2010

How to modify URL in QuickLaunch links programmatically

A MOSS 2007 project has been deployed into production and I need to provide them a patch to fix up some Quick Launch links and some other functionality. The only way I had is to build a WSP with code to make my changes to the Serve where ever it is deployed. During achieving QuickLaunch Fixes I faced a little issues and decided to publish the solution to help others

We can access the quick launch from the "navigation" object available in "web" object.

web.Navigation.QuickLaunch
The QuickLaunch object is of type SPNavgationNodeCollection wich contains the collection of SPNavigationNode. There is a built in method "Update" provided in SPNavigationNode class to save any changes made in SpNavigationObject properties to the database. I tried to use the method after updating the URL of QuickLaunch link

SPNavigationNode Node = web.Navigation.QuickLaunch[Counter];
Node.Url = "NewURL/Confirmation.aspx";
Node.Update();
But it didn't work for me. I googled this issue a lot but no luck :(. After a lot of thinking on this issue and exploring the Update method using reflector I came to know that update method actually calls UpdateNavigationNode() that resides inside SPRequest object.

this.Navigation.Web.Request.UpdateNavigationNode(this.Navigation.Web.Url, this.Id, this.m_dtParented, this.Title, this.Url, ref this.m_PropertyArray, out str)





Unfortunately SPRequest object is marked with "internal" access modifier and we can not access it from another assembly. but i have to call it directly to update the content database with my URL change so I decided to use reflection to access the internal object

Object Request = null;
Request=web.GetType().GetField("m_Request",System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance).GetValue(web);

After successfully getting the Request object I also need to execute the UpdateNavigationNode() method using reflection because it is also marked with "internal" access modifier

Request.GetType().GetMethod("UpdateNavigationNode").Invoke(Request, new Object[] { node.Navigation.Web.Url, node.Id, node.LastModified, node.Title, URL, (m_PropertyArray), str });
Finally the method looks like this
void UpdateQuickLaunchNodeURL(SPWeb web, SPNavigationNode node, string URL)
{
Object Request = null;
Request = web.GetType().GetField("m_Request", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(web);
object m_PropertyArray = new object();
string str = "";
Request.GetType().GetMethod("UpdateNavigationNode").Invoke(Request, new Object[] { node.Navigation.Web.Url, node.Id, node.LastModified, node.Title, URL, (m_PropertyArray), str });
web.Update();
}
And this code worked for definitely. Hope it will help others.

Friday, June 18, 2010

how to match lookup value using CAML

When you query a Lookup field using CAML by default it will be executed for matching the text of lookup that is not desired in many cases like my.

<Query>
<Where>
<Eq>
<FieldRef Name="LookupFieldName" />
<Value Type="Lookup">1</Value>
</Eq>
</Where>
</Query>

We need to just set a property "LookupId" to true in FieldRef Tag like the following


<Query>
<Where>
<Eq>
<FieldRef Name="LookupFieldName" LookupId="True" />
<Value Type="Lookup">1</Value>
</Eq>
</Where>
</Query>

This will instruct the CAML engine to use the value property of lookup to execute the match.

Thursday, June 17, 2010

Setting 20+ Lookup value at client side, JQuery

I have faced the issue many time when setting the value into sharepoint controls at client side. Here i will explain how to set the value in 20+ lookup control at client side using JQuery. First I tried to setting a value into its textbox and then firing a change event of textbox but that doesn't work. If you use JQuery to raise change event like this

$("[Title='TitleOfControl']").val("NewValue").change();

This event carry the srcElement property null that results in crash of "HandleChange" function in the start.

And if you try to raise the event like this

$("[Title='TitleOfControl']")[0]fireEvent('onchange');

There are many prerequisites to this approach like you have to first raise the click of lookup that will render the options in the DOM internally and then you have to select the appropriate option like this

var LookupInput = $("[Title='TitleOfControl']");
LookupInput.next().click();
$("select[id='" + LookupInput.attr("opt") + "']").find("option[value='"+ SelectedValue +"']").attr("selected","true");

and then raise the change event. This approach will only work if there are no duplicate text in the lookup otherwise it will fail.

Finally then i come to the following straight forward and working solution

var LookupInput = $("[Title='TitleOfControl']");
LookupInput.attr("match","SelectedText");
LookupInput.val("SelectedText");
$("input[id='" + LookupInput.attr("opthid") + "']").val("SelectedValue");

In this solution you need to set the value in TextBox, "opt" attribute of TextBox and in Hidden field "opthid" that is mentioned in attribute of TextBox. This solution worked for me like charm. Hope it will help others too.

HandleChange function that is called when lookup's textbox value changes
function HandleChange()
{
var ctrl=event.srcElement;
var str=ctrl.value;
var opt=document.getElementById(ctrl.opt);
ctrl.match=FilterChoice(opt, ctrl, str, "");
}

Writing Better SharePoint Code

http://www.sharepointproconnections.com/article/sharepoint/Writing-Better-SharePoint-Code.aspx

Wednesday, May 19, 2010

Summerizing all worksheet columns into first worksheet


Sub Macro1()


Dim ws As Worksheet
Dim wsInfo As Worksheet
Set wsInfo = ActiveWorkbook.Sheets(1)
Dim Counter As Integer
Counter = 1
For Each ws In ActiveWorkbook.Sheets

Dim columnCounter As Integer
Dim wsColumnCounter As Integer
columnCounter = 2
wsInfo.Cells(Counter, "A").Value = ws.Name
wsColumnCounter = 1
Do While ws.Cells(1, i) <> ""

wsInfo.Cells(Counter, columnCounter).Value = ws.Cells(1, wsColumnCounter)
columnCounter = columnCounter + 1
wsColumnCounter = wsColumnCounter + 1

Loop
Counter = Counter + 1

Next

End Sub

Installing Microsoft Office Sharepoint 2007 (MOSS 2007)

http://www.datasprings.com/Resources/ArticlesInformation/OverviewonInstallingSharepoint2007/tabid/774/Default.aspx

Thursday, April 22, 2010

Enable Ajax in SharePoint on Custom Forms

In order for Ajax to work properly, we need to edit init.js file installed with MOSS 2007 on server. The file is located at: Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033.

Open the file and go to method _spFormOnSubmitWrapper(), the following code will be found in the file:

if (_spSuppressFormOnSubmitWrapper)
{
return true;
}
if (_spFormOnSubmitCalled)
{
return false;
}

Replace the above code with the following code:

if (_spSuppressFormOnSubmitWrapper)
{
return true;
}
if (_spFormOnSubmitCalled)
{

//return false;

}
Note that “return false” statement has been commented out. This will make ajax calls to work properly.


Refrence:

http://fahadkundi.wordpress.com/2009/05/26/enable-ajax-in-sharepoint-on-custom-forms/

Tuesday, April 20, 2010

SharePoint Deployment Methods Comparison (Site/SubSite)

Recently i was assigned a task to compare the different deployment methods for SharePoint site/subsite. I searched over web but found nothing in a single piece. I combined the results from different sources and shaped the following

  1. Import/Export

    • Pros
      • Allows incremental releases (patches)
      • Users and Security inclusion is optional
      • Can carry site structure as well as data
      • Import on same level as the export was prepared
    • Cons
      • Before running selective migrations, you must run a full migration in order to set up your destination server
      • Complex procedure of preparing Import and export packages for incremental releases
      • Site Name and description will remain same and cannot be changed during import

    Selective Migration: Export Flowchart
























  2. Backup/Restore

    • Pros
      • Easy to use
      • One step command for backup and one for step restore
      • Can carry site structure and data as well
    • Cons
      • Users and Security will be included automatically
      • Only restorable on Root Site (Even sub site back will be restored on root site)
      • Site Name and description will remain same and cannot be changed during restore
  3. STP
    • Pros
      • Site Name and description can be changed during site creation with STP
      • Users and Security inclusion is optional
      • Can carry site structure as well as data
      • Can be deployed on any level regardless of source site level of STP
    • Cons
      • No incremental release supported
      • Does not include Users and Security
      • Inclusion of data is optional

References:

http://msdn.microsoft.com/en-us/library/cc768566.aspx
http://onetidbit.wordpress.com/

http://msdn.microsoft.com/en-us/library/bb530303.aspx

Standard Electrical Formulas Used for Power Consumption Calculations

TO DETERMINE:

SINGLE-PHASE

THREE-PHASE

DIRECT
CURRENT

KVA

I x E
1000

I x E x 1.73
1000

--------

Kilowatts

I x E x PF
1000

I x E x 1.73 x PF
1000

I x E
1000

Horsepower

I x E x %EFF x PF
746

I x E x 1.732 x %EFF x PF
746

I x E x %EFF
746

Amperes (when HP is known)

HP x 746
E x %EFF x PF

HP x 746
1.73 x E x %EFF x PF

HP x 746
E x %EFF

Amperes (when kW is known)

KW x 1000
E x PF

KW x 1000
1.73 x E x PF

KW x 1000
E

Amperes (when KVA is known)

KVA x 1000
E

KVA x 1000
1.73 x E

--------

1 HP = 7457 Watts

1000 Watts (1KW) = 1.25 KVA with PE=0.8

1 KW = 1 KVA with PE=1.0

Usually observed in generators

1 HP = 571.42

No PE given