Monday, August 3, 2015

SharePoint Cross Site Lookup Field

Can we create a cross site lookup field?
Yes of course, it can be. Thought it is not possible OTB, a simple trick will do this.

Scenario: Creating a cross site lookup field between the two peer web sites or sub web sites under a same site collection.
Example: Create a lookup field between two sites http:/sitecollection/webA & http://sitecollection/webB

The trick is just to set the lookup field webId with the target list webId.
Code snippet to create cross site lookup field programatically:
private bool CreateCrossSiteLookupField(SPWeb sourceWeb, SPWeb targetWeb, string sourceListTitle, string targetListtitle, string lookUpFieldName, string lookupValueColumn, bool isRequiredField)
{
    ////Get the source and target lists 
    SPList sourceList = sourceWeb.Lists.TryGetList(sourceListTitle);
    SPList targetList = targetWeb.Lists.TryGetList(targetListtitle);

    if (null != sourceList && null != targetList)
    {
        ////Add a lookup field to source list with target list as lookup list
        sourceList.Fields.AddLookup(lookUpFieldName, targetList.ID, isRequiredField);

        ////Get the created lookup field from source list 
        SPFieldLookup lookupField = (SPFieldLookup)sourceList.Fields[lookUpFieldName];

        ////Set the lookup field's webID with target list webId
        lookupField.LookupWebId = targetList.ParentWeb.ID;

        ////Set the lookup field's display field 
        lookupField.LookupField = targetList.Fields[lookupValueColumn].InternalName;

        ////Finally update the lookup field
        lookupField.Update();
    }
    return true;
}
Code snippet to create cross site lookup field with PowerShell:
Add-PSSnapIn "Microsoft.SharePoint.Powershell"

#Get the webs and lists
$webA = Get-SPWeb http://siteurl/webA/
$webB = Get-SPWeb http://siteurl/webB/
$sourceList = $webA.Lists.item("SourceList")
$targetList = $webB.Lists.item("TargetList")

#Add a lookup field to source list with target list as lookup list
$sourceList.fields.AddLookup("LookupFieldName", $targetList.id, "true")

#Get the created lookup field from source list
$lookupField = $sourceList.Fields["LookupFieldName"]

#Set the lookup field's webID with target list webId
$lookupField.LookupWebId = $targetList.ParentWeb.ID

#Set the lookup field's display field
$lookupField.LookupField = $targetList.Fields["Title"].InternalName
$lookupField.Update();

Saturday, August 1, 2015

JQuery Set Checkbox checked vs RadioButton checked

It is a common mistake or confusion on how to set value of Checkbox vs. RadioButton to checked using JQuery. Devs often use .attr method to set the value for both checkbox and radiobutton controls. Note: Using .attr on checkbox is deprecated.

However the right way to set checkbox checked is by using .prop method and radiobutton checked is by using .attr method respectively.

CHECKBOX:
$("#checkBoxCtrlId").prop("checked", true);
$("#checkBoxCtrlId").prop("checked", false);
RADIOBUTTON:
$("#radioButtonCtrlId").attr("checked", true);
$("#radioButtonCtrlId").attr("checked", false);
However same attribute will be used to check either checkbox or radiobutton is checked.
$('.checkBoxCtrlId').is(':checked');
$('.radioButtonCtrlId').is(':checked');

How to View SharePoint App Web Site Contents

I know most of the SharePoint App developers or users would definitely look for site contents of an App Web and List Settings in an App Web. But it turns out that there is no direct way to access the App Web Contents similar to a regular site's Site Contents.

However I have a trick to see the app web site contents by typing in the below URL as a suffix to your app web URL.
/_layouts/15/mcontent.aspx

Example: http://app-web-url/_layouts/15/mcontent.aspx

It lists all the available lists/libraries in the app web. But you will notice that the way the contents are listed is different from the regular Site Contents and by clicking on the list/library will redirects to list/library settings instead actual list. On the settings page you see the List Name which redirects to the actual list content.

Hope this post is helpful...