Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

SharePoint CAML Query Builder Dialog for your Web Parts

Rate me:
Please Sign up or sign in to vote.
4.65/5 (9 votes)
24 Mar 2009CPOL7 min read 167.7K   847   34  
A SharePoint CAML query builder dialog for your Web Parts
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web;

namespace Mullivan.SharePoint.WebParts
{
    public class LibNavLinksEditor : EditorPart
    {
        private TextBox _tboxViewState;
        private Literal _litHtml;
        private string _startUpScriptName;

        public LibNavLinksEditor(string webPartId)
        {
            this.ID = webPartId + "_LibNavLinksEditor";
            _startUpScriptName = this.ID + "_Init";
        }

        protected override void OnInit(EventArgs e)
        {
            _tboxViewState = new TextBox();
            _tboxViewState.ID = "lnwpEditorViewState";
            _tboxViewState.Style[HtmlTextWriterStyle.Visibility] = "hidden";
            _tboxViewState.Style[HtmlTextWriterStyle.Display] = "none";

            _litHtml = new Literal();
            _litHtml.Text = Properties.Resources.LibraryNavigation;

            this.Style[HtmlTextWriterStyle.TextAlign] = "left";

            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Add(_tboxViewState);
            this.Controls.Add(_litHtml);
        }

        protected override void OnLoad(EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
            Type lnType = this.GetType();

            string strEditUrl = csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.WebParts.Images.Edit.png");
            string strAddUrl = csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.WebParts.Images.Add.png");
            string strRemoveUrl = csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.WebParts.Images.Remove.png");
            string strArrowUpUrl = csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.WebParts.Images.ArrowUp.png");
            string strArrowDownUrl = csm.GetWebResourceUrl(lnType, "Mullivan.SharePoint.WebParts.Images.ArrowDown.png");

            EnsureChildControls();

            _litHtml.Text = string.Format(_litHtml.Text,
                this.ClientID,
                _tboxViewState.ClientID,
                strAddUrl,
                strRemoveUrl,
                strArrowUpUrl,
                strArrowDownUrl,
                strEditUrl,
                SPContext.Current.Web.ServerRelativeUrl.Replace("/", "\\u002f"));

            base.OnLoad(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
            Type lnType = this.GetType();
            //Register our Javascript file
            if (!csm.IsClientScriptIncludeRegistered(@"Mullivan.SharePoint.WebParts.JS.Utility.js"))
            {
                string url = csm.GetWebResourceUrl(lnType, @"Mullivan.SharePoint.WebParts.JS.Utility.js");
                csm.RegisterClientScriptInclude(lnType, "Mullivan.SharePoint.WebParts.JS.Utility.js", ResolveClientUrl(url));
            }

            if (!csm.IsClientScriptIncludeRegistered(@"PickerTreeDialog"))
            {
                string url = @"/_layouts/1033/PickerTreeDialog.js";
                csm.RegisterClientScriptInclude(lnType, "PickerTreeDialog", url);
            }

            if (!csm.IsClientScriptIncludeRegistered(@"QueryBuilderDialog"))
            {
                string url = @"/_layouts/1033/QueryBuilder.js";
                csm.RegisterClientScriptInclude(lnType, "QueryBuilderDialog", url);
            }

            if (!csm.IsStartupScriptRegistered(_startUpScriptName))
            {
                csm.RegisterStartupScript(lnType, _startUpScriptName, GenerateStartScript());
            }

            base.OnPreRender(e);
        }

        private string GenerateStartScript()
        {
            return string.Format(@"
<script type=""text/javascript"">
LPNW_InitConfiguration('{0}', '{1}');
</script>"
                ,_tboxViewState.ClientID, this.ClientID);
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            LibraryNavigationWebPart lnwp = this.WebPartToEdit as LibraryNavigationWebPart;
            if (lnwp != null)
                lnwp.LinksConfiguration = this._tboxViewState.Text;

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();

            LibraryNavigationWebPart lnwp = this.WebPartToEdit as LibraryNavigationWebPart;
            if (lnwp == null)
                this._tboxViewState.Text = "<Links></Links>";
            else
                this._tboxViewState.Text = lnwp.LinksConfiguration;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions