Click here to Skip to main content
15,879,184 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 166.9K   847   34  
A SharePoint CAML query builder dialog for your Web Parts
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using System.Text;
using System.Collections.Generic;
using System.Xml;
using System.Reflection;
using Mullivan.SharePoint.Pages.WebControls;
using Mullivan.SharePoint.WebControls;

namespace Mullivan.SharePoint.Pages
{
    public partial class _FieldValueDialog : System.Web.UI.Page
    {
        private ClientScriptManager _csm = null;
        private Type _type = null;
        private SPList _list = null;
        private SPField _spField = null;
        private SPFieldRenderer _control = null;

        public string ListUrl
        {
            get
            {
                if (this.ViewState["ListUrl"] != null)
                    return (string)this.ViewState["ListUrl"];
                else
                    return string.Empty;
            }
            set
            {
                this.ViewState["ListUrl"] = value;
            }
        }

        public string EditField
        {
            get
            {
                if (this.ViewState["EditField"] != null)
                    return (string)this.ViewState["EditField"];
                else
                    return string.Empty;
            }
            set
            {
                this.ViewState["EditField"] = value;
            }
        }

        public string InitialValue
        {
            get
            {
                if (this.ViewState["InitialValue"] != null)
                    return (string)this.ViewState["InitialValue"];
                else
                    return string.Empty;
            }
            set
            {
                this.ViewState["InitialValue"] = value;
            }
        }

        public _FieldValueDialog()
        {
            _csm = Page.ClientScript;
            _type = typeof(_FieldValueDialog);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            queryBuilderIcon.ImageUrl = ResolveUrl(_csm.GetWebResourceUrl(_type, "Mullivan.SharePoint.Pages.Images.edit32.png"));
            dialogDescription.Text = "Use this page to edit a value for a List Field.";
            dialogTitle.Text = "Field Editor - {0}";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ((DialogMaster)base.Master).OkButton.Click += new EventHandler(OkButton_Click);
            if (base.IsPostBack)
            {
                if (!SPUtility.ValidateFormDigest())
                {
                    throw new SPException("Security Validation Failed");
                }

                _list = GetList(this.ListUrl);

                if (_list == null)
                    throw new SPException("List not found.");
            }
            else
            {

                this.ProcessQueryString();

                _list = GetList(this.ListUrl);

                if (_list == null)
                    throw new SPException("List not found.");
            }
            _spField = _list.Fields.GetFieldByInternalName(this.EditField);

            if (_spField != null)
            {
                lblColumnName.Text = _spField.Title;
                lblColumnType.Text = _spField.TypeDisplayName;
                dialogTitle.Text = string.Format(dialogTitle.Text, _spField.Title);

                _control = new SPFieldRenderer();
                _control.Field = _spField;
                _control.List = _list;

                pnlEditContainer.Controls.Add(_control);

                if(!Page.IsPostBack)
                    _control.Value = this.InitialValue;

            }
            else
                throw new Exception(string.Format("Field {0} could not be found.", this.EditField));
        }

        private void ProcessQueryString()
        {
            string strListUrl = string.Empty;
            string strField = string.Empty;
            string strInitialValue = string.Empty;

            if (string.IsNullOrEmpty(strListUrl = Request.QueryString["list"]))
                throw new SPException("List not found.");
            if (string.IsNullOrEmpty(strField = Request.QueryString["field"]))
                strField = string.Empty;
            if (string.IsNullOrEmpty(strInitialValue = Request.QueryString["value"]))
                strInitialValue = string.Empty;

            this.InitialValue = strInitialValue;
            this.ListUrl = strListUrl;
            this.EditField = strField;
        }

        private SPList GetList(string listUrl)
        {
            
            SPSite site = SPControl.GetContextSite(this.Context);

            using (SPWeb web = site.OpenWeb(GetWebUrl(listUrl)))
            {
                string listName = GetListName(listUrl);
                return web.Lists[listName];
            }
        }

        private string GetWebUrl(string listUrl)
        {
            string webUrl = string.Empty;
            listUrl = listUrl.TrimEnd('/');
            int lastIndex = listUrl.LastIndexOf("/");
            if (lastIndex > -1)
                webUrl = listUrl.Substring(0, lastIndex);

            return webUrl.TrimEnd('/');
        }

        private string GetListName(string listUrl)
        {
            string listName = string.Empty;
            listUrl = listUrl.Trim('/');
            int lastIndex = listUrl.LastIndexOf("/");
            if (lastIndex > -1)
                listName = listUrl.Substring(lastIndex + 1, listUrl.Length - lastIndex - 1);

            return listName;
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            _control.Validate();
            if (!_control.IsValid)
                return;

            string val = _control.Value;
            if (!string.IsNullOrEmpty(val))
                val = val.Replace("'", "\\'");
            else
                val = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\" Language=\"javascript\">");
            sb.AppendLine(string.Format("HandleOkClicked('{0}');", val));
            sb.AppendLine("</script>");

            _csm.RegisterStartupScript(_type, "FieldEditSetOk", sb.ToString());

        }

    }
}

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