Thursday, April 28, 2011

Enable Ribbon button when one item only is selected and it is not folder

Objective:
To programmatically enable a ribbon button by JavaScript, when one item only is selected.

Solution:
Edit the element.xml file of the Ribbon button Custom Action.
In the CommandUIHandles section

<CommandUIHandlers>
        <CommandUIHandler
                Command="CommandName"
                CommandAction=""
                EnabledScript=" …  "/>
      </CommandUIHandlers>
</CommandUIHandlers>

Add the following JavaScript code in EnabledScript:

EnabledScript="javascript:
        function enable() {
        var items = SP.ListOperation.Selection.getSelectedItems();
        var itemCount = CountDictionary(items);
        return (itemCount==1 &amp;&amp; items[0].fsObjType == 0);
        }enable();

itemCount==1  is used to identify if one item only is selected

If this condition is TRUE it means that the “items” array contains the element 0.
So it is possible to test if is it a folder with the following:

 items[0].fsObjType == 0

If you write you JavaScript code in the Element.xml file, you need to encode the operator AND: &amp;&amp;

Saturday, April 16, 2011

Show a document library hierarchy in a tree view

Objective:
To programmatically build a tree view that shows a specific document library.
Solution:
To achieve this goal, SharePoint offers the classes:
- SPHierarchyDataSourceControl
- SPTreeView
With SPHierarchyDataSourceControl you can build the entire hierarchy of you web. In this case we need to limit it to a single document library.
Build a new Web Part and override the CreateChildControls method with the following code, modifying the data source with your needed:

protected override void CreateChildControls()
{
           
        SPDocumentLibrary docLib = GetDocLib(SPContext.Current.Web.ServerRelativeUrl, "Shared Documents");
        //change GetDocLib with your code to get a SPDocumentLibrary object

SPHierarchyDataSourceControl myDataSource = new SPHierarchyDataSourceControl();
myDataSource.ID = "myTreeViewDataSource";
myDataSource.RootContextObject = "Web";
        myDataSource.IncludeDiscussionFolders = false;
        myDataSource.ShowDocLibChildren = true;
        myDataSource.ShowFolderChildren = true;
        myDataSource.ShowListChildren = false;
        myDataSource.ShowWebChildren = false;
        myDataSource.Web = SPContext.Current.Web;
        myDataSource.RootWebId = SPContext.Current.Web.ID.ToString();
        myDataSource.RootContextObject = null;
        myDataSource.RootListId = docLib.ID.ToString();
           

        SPTreeView myTreeView = new SPTreeView();
        myTreeView.ID = "myTreeView";
        myTreeView.DataSourceID = "myTreeViewDataSource";
        myTreeView.EnableClientScript = true;
        myTreeView.EnableViewState = true;
        myTreeView.NodeIndent = 12;
        myTreeView.NodeStyle.CssClass = "ms-navitem";
        myTreeView.NodeStyle.HorizontalPadding = 2;
        myTreeView.SelectedNodeStyle.CssClass = "ms-tvselected";
        myTreeView.ExpandDepth = 0;
        myTreeView.ExpandImageUrl =
        SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/tvplus.gif");

        myTreeView.CollapseImageUrl =
        SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/ tvminus.gif");

        myTreeView.NoExpandImageUrl =
       SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl, "/_layouts/images/ tvblank.gif");

        myTreeView.SkipLinkText = "";
        myTreeView.ShowLines = true;
        Controls.Add(myTreeView);
}

Monday, April 11, 2011

Open SharePoint 2010 modal dialog and refresh the parent page from custom form

Objective:
1) Open a SharePoint 2010 popup and refresh the parent page when one closes it.
2) Build a custom form that at “submit event” closes the popup and refreshes the parent page.
Solution:
-          Open a Popup using “Javascript” and “SP.UI.$create_DialogOptions();”
-          Add the method “CloseCallback” using the “dialogReturnValueCallback” property
-          Add some JavaScript code inside the CloseCallback to reload the parent page, like the following example:

<a href="javascript:openModalDialog('form.aspx');">Open My Custom Form</a>

<SharePoint:ScriptLink ID="ScriptLink1" Name="sp.js" runat="server" OnDemand="true" Localizable="false" />

<script type="text/ecmascript">

    var options;
    function openModalDialog() {
        options = SP.UI.$create_DialogOptions();
        options.width = 300;
        options.height = 100;
        options.url = SP.Utilities.Utility.getLayoutsPageUrl('customdialog.htm' );
        options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
        SP.UI.ModalDialog.showModalDialog(options);

    }

    function CloseCallback(result, target) {
        location.reload(true);
    }

</script>

Now when you close the popup, the parent page is refreshed.
If one is working with a Custom Form and wants to close the popup and refresh the parent after the Submit,
he could have two (or more) choices:
1) If you have 1 post back only, after the submit, the you can add in the page load the following:
protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                string script = "<script language='javascript'>parent.CloseCallback();</script>";
                if (!this.Page.ClientScript.IsClientScriptBlockRegistered("rKey"))
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "rKey", script);
            }
        }

          2) If your custom form handles more than 1 post back, you can add the following code at one specific submit click event:
Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Context.Response.Flush();
Context.Response.End();