Archive for the ‘MOSS 2007’ Category

How To: add Return URL for SharePoint form action buttons

February 2, 2014 2 comments


You must have seen SharePoint forms with ‘Save’ or ‘Cancel’ buttons.

You can specify return url for the same. it means, after save or cancel click, browser will be redirect to return URL which is defined in Query string.

For example:


It also supports, Relative URL like,


Here, it will redirect to default page after completing form submitting.

Keep SharePointing…Keep Rocking…!!!

How To: Create CAML Query Dynamically in SharePoint

December 26, 2013 12 comments

In one of my project, I was having requirement where we must need to create CAML query dynamically.

So I’ve tried to googling..and i found some good options but it does not fulfill my requirement.

So  I’ve created my own by taking some reference.

Might be useful to someone like me:

public static string CreateCAMLQuery(List<string> parameters, string orAndCondition, bool isIncludeWhereClause)
 StringBuilder sb = new StringBuilder();

 if (parameters.Count == 0)
AppendEQ(sb, "all");

int j = 0;
for (int i = 0; i < parameters.Count; i++)
   if (!string.IsNullOrEmpty(parameters[i].Split(';')[3]))
    AppendEQ(sb, parameters[i]);

    if (i > 0 && j > 0)
     sb.Insert(0, "<" + orAndCondition + ">");
     sb.Append("</" + orAndCondition + ">");
 if (isIncludeWhereClause)
  sb.Insert(0, "<Where>");
 return sb.ToString();

public static void AppendEQ(StringBuilder sb, string value)
 string[] field = value.Split(';');

 sb.AppendFormat("<{0}>", field[2].ToString());
 sb.AppendFormat("<FieldRef Name='{0}'/>", field[0].ToString());
 sb.AppendFormat("<Value Type='{0}'>{1}</Value>", field[1].ToString(), field[3].ToString());
 sb.AppendFormat("</{0}>", field[2].ToString());

Now, you can prepare your query and call the function as per below to create dynamic query:

List<string> objColumns = new List<string>();

// here, format is like: "Column name(internal name of column);Column type; Conditional operator; Search string (input by user)

objColumns.Add("Title;Text;Contains;" + textbox1.Text.Trim());

objColumns.Add("FileLeafRef;Text;Contains;" + textbox1.Text.Trim());

SPQuery query = CreateCAMLQuery(objColumns, "Or", true);

Let me know your views or feedback.

Happy coding and SharePointing !

How To: Remove webpart when it’s giving error

November 2, 2011 Leave a comment


Sometimes, we add any webpart on any page. And if it is having error, it shows only error.

Now, What to do if I want to remove or close those webparts and want to work my Page.

Just, add “?contents=1” in your page URL.



It will show you Maintenance Page, there you will see list of all webparts.

And you can close or remove webparts from there.

Happy Coding !!!

Categories: MOSS 2007, Sharepoint 2010

How To: Export Site Collection To XML

October 22, 2011 2 comments

Recently, I was working on project. And I need to know how many sites, lists available in our site collection.

I was looking out put in XML format.

So, I write utility which takes input of site collection and it export structure of site collection and lists.

public static void ExportToXML(string url)
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            XmlNode sitesNode = doc.CreateElement("Site");
            XmlAttribute x_Url = doc.CreateAttribute("url");
            x_Url.Value = url;

            XmlAttribute copygroupattr = doc.CreateAttribute("CopyGroups");
            copygroupattr.Value = "true";

            SPSite site = new SPSite(url);
            // Console.WriteLine("<Site>");
            foreach (SPWeb web in site.AllWebs)
                XmlNode webnode = doc.CreateElement("Web");
                XmlAttribute titleattribute = doc.CreateAttribute("title");
                titleattribute.Value = (web.Name.Length > 0) ? web.Name : web.Title;

                XmlAttribute weburlattribute = doc.CreateAttribute("url");
                weburlattribute.Value = web.Url;

                Console.WriteLine("<web title=\"" + web.Name + "\" url=\"" + web.Url + "\">");
                foreach (SPList list in web.Lists)

                    XmlNode listnode = doc.CreateElement("list");
                    XmlAttribute listattribute = doc.CreateAttribute("title");
                    listattribute.Value = list.Title;

                    XmlAttribute listurlattribute = doc.CreateAttribute("url");
                    listurlattribute.Value = list.DefaultViewUrl;
                    if (list.EventReceivers.Count > 0)
                        XmlAttribute listEventattribute = doc.CreateAttribute("EventHandler");
                        listEventattribute.Value = "true";
                    XmlAttribute listGuidattribute = doc.CreateAttribute("Guid");
                    listGuidattribute.Value = list.ID.ToString();

                    Console.WriteLine("<list title=\"" + list.Title + "\" url=\"" + list.DefaultViewUrl + "\"  Guid=\"" + list.ID.ToString() + "\">");
                    //SPFieldCollection fields = list.Fields;
                    //foreach (SPListItem item in list.Items)
                    //    Console.WriteLine("<item>");
                    //    foreach (SPField field in fields)
                    //    {
                    //        //note put a check for null field values here
                    //        Console.WriteLine("<property title=\"" + field.Title + "\" value=\"" + item[field.Id].ToString() + "\"/>");
                    //    }
                    //    Console.WriteLine("</item>");



Happy Coding!!!

How To: check SPList is available or not in web?

April 26, 2011 Leave a comment

Solution is given for MOSS 2007 and SharePoint 2010.

MOSS 2007

SPSite Currentsite = SPContext.Current.Site;
SPWeb Currentweb = CurrentSite.OpenWeb();
protected void Page_Load (object sender, EventArgs e)
        SPList list = Currentweb.Lists[“YourListName”];
public static bool TryGetList(string Listname, string SharepointURL)
 bool retVal;

using (SPSite site = new SPSite(SharepointURL))
 using (SPWeb web = site.RootWeb)
 SPList SPEConfigList = web.Lists[Listname];
 retVal = true;
 catch (Exception ex)
 retVal = false;
 return retVal;


Now, the same thing in SharePoint 2010,

protected void Page_Load (object sender, EventArgs e)
   SPList list = CurrentWeb.Lists.TryGetList((“YourListName”));
   If(list != null)
      // Do Your Stuff;

Happy Coding…!!!

Categories: MOSS 2007, Sharepoint 2010