Friday, November 2, 2012

How to query two or more columns of SharePoint list with multiple Values.

Here I am going to explain a you a way to create a dynamic query which deals with multiple values of two fields(Columns). There are other way of doing it to do it through LINQ but that would be heavy because you have to use for each loop for the purpose.

I will do it for two list of string values collection for two columns. I am considering AND operation between columns and OR between all values for columns. I will divide into three function for clarity. However I and II are same except its filed name, you can modify it as per your need. III function would be combine both these queries with an AND operator.

1. First we will create dynamic query for Column1:

 public StringBuilder GetQueryForColumn1(List<string> Values1)
        {
            StringBuilder stbQuery = new StringBuilder();
            if (Regions.Count > 0)
            {
                bool first = true;
                    foreach (string Value in Values1)
                    {

                        if (first)
                        {
                            stbQuery.Append("<Eq><FieldRef Name='Column1' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            first = false;
                        }
                        else
                        {
                            string formatedQuery = stbQuery.ToString();
                            StringBuilder objNew = new StringBuilder();
                            objNew.Append("<Or>");
                            objNew.Append("<Eq><FieldRef Name='Column1' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            objNew.Append(formatedQuery);
                            objNew.Append("</Or>");
                            stbQuery = objNew;
                        }
                }
            }
            return stbQuery;
        }


2. Now we will create dynamic query for Column2:

 public StringBuilder GetQueryForColumn2(List<string> Values2)
        {
            StringBuilder stbQuery = new StringBuilder();
            if (Regions.Count > 0)
            {
                bool first = true;
                    foreach (string Value in Values2)
                    {

                        if (first)
                        {
                            stbQuery.Append("<Eq><FieldRef Name='Column2' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            first = false;
                        }
                        else
                        {
                            string formatedQuery = stbQuery.ToString();
                            StringBuilder objNew = new StringBuilder();
                            objNew.Append("<Or>");
                            objNew.Append("<Eq><FieldRef Name='Column2' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            objNew.Append(formatedQuery);
                            objNew.Append("</Or>");
                            stbQuery = objNew;
                        }
                }
            }
            return stbQuery;
        }


3. Then we will put this into a common function and concatenate  it : 

 public StringBuilder GetQueryForBothColumn1AndColumn2(List<string> Regions, List<string> OfficerTitles)
        {
            StringBuilder stbAllQuery = new StringBuilder();
            stbAllQuery.Append("<Where><And>");
            stbAllQuery.Append( GetQueryForColumn1(Values1));
            stbAllQuery.Append( GetQueryForColumn2(Values2));
            stbAllQuery.Append("</And></Where>");
            return stbAllQuery;
        }

 
Now this query you can use to query on this on your chunk of selected choices in a single query to list. you can do it for more than two columns as well but that would need a bit effort and change in  GetQueryForBothColumn1AndColumn2 function.




Wednesday, October 31, 2012

How to manage and query items in folders of SharePoint Lists pro-grammatically in C#.

Here I am going to provide you very simple way of managing your SharePoint List Items in Folders. In this way we will first check that folder with this name exists or not. If it exists then it will insert item in that folder and if not then it will create a new folder of this name and put item inside this newly created folder. 

At first we need to include this method in our project to check that particular folder exists or not :

 public static bool FolderExists(string url, SPWeb web)
        {
            try
            {
                if (web.GetFolder(url).Exists)
                {
                    return true;
                }
                return false;
            }
            catch (ArgumentException ex)
            {
                Logger.Write(ex);
                return false;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }


This methods will check that a folder with the supplied parameters exists or not. And then add item as per values given in corresponding folder OR if does not exists will create a new one  and then add item as per values given in corresponding folder.  

public void UpdateDemoFolderInListA(SPWeb objSPWeb, string strDemo)
        {
            if (objSPWeb != null)
            {
                SPList objSPListListA = objSPWeb.Lists.TryGetList("ListA");
                if (objSPListSavedSearch != null)
                {
                    SPFolder spFolder;
                    if (FolderExists(objSPWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo, spWeb))
                    {
                        spFolder = spWeb.GetFolder(objSPWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo);
                    }
                    else
                    {
                        SPListItem newFolder = objSPListSavedSearch.Items.Add(spWeb.ServerRelativeUrl + "/" + objSPListListA.RootFolder.Url, SPFileSystemObjectType.Folder, strDemo);
                        newFolder.Update();
                        spFolder = spWeb.GetFolder(spWeb.ServerRelativeUrl + "/Lists/ListA/" + strDemo);
                    }
                        SPListItem item = objSPListListA.Items.Add(spFolder.ServerRelativeUrl, SPFileSystemObjectType.File);
                        item["Column1"] = Value1;
                        item["Column2"] = Value2;
                        item["Column3"] = Value3;
                        item.Update();
                    }
                }
            }
        }


You can use this approach to extend functionality such as to add item in some restriction such as maximum no. of items in the folder or date wise folder or user logged in folder items update.Anyways querying for item inside a particular folder with defined URL will a performance boost.  Hope it will help.

Tuesday, October 30, 2012

How to add a border less poopup in an asp.net or SharePoint Page.

It is quite tedious to add popup in your page without any border. And more than that if you have to transfer a lot of information. Sometimes we need to add a new Page in our project just for adding a very simple popup which contains a text box only. However putting all theses you can not get rid of windows default close and minimize button. So I have done this through putting a div and then setting its z-index style. Through which It will look like a popup.

Here are the steps to integrate this solution into your page.

Code need to put into .aspx or .ascx file :-

Code to put add a panel and controls : 

 <asp:Panel runat="server" ID="pnlPopupContainer" CssClass="positionSetter">
        <table width="100%">
            <tr style="width: 100%">
                <td style="text-align: left; vertical-align: middle; width: 300px; padding-left: 10px;">
                    <asp:TextBox runat="server" ID="txtBox1" Width="275px"></asp:TextBox>
                </td>
                <td style="vertical-align: middle; width: 300px;">
                    <asp:LinkButton runat="server" Text="
Save Search" ID="lnkBtn1"                                  OnClientClick="HideMainPanel()" OnClick="lnkBtn1_Click"></asp:LinkButton>
                </td>
            </tr>
        </table>
    </asp:Panel>


 This is the link on which click popup will be opened. It can be a button or anything you want.
<a href='#' onclick='ShowPanel()'><b>Open Popup </b></a>

 Styles :  

      .positionSetter       
      {
            position: absolute;
            left: 275px;
            top: 425px;
            width: 400px !important;
            height: 35px !important;
            text-align: center;
            z-index: 1000;
            background-color: #4f81bc !important;
            visibility: hidden;
        }

This style class will play a very crucial role in its positioning.  Left and top attribute will set its horizontal position in page, z-index will position its over page and give a popup feel, width and height will decide popup width and height. 

JavaScript :
This java script functions will show and hide of our div(popup).

function HideMainPanel() {
            el = document.getElementById('<%=
pnlPopupContainer.ClientID%>');
            if (el != null) {
                el.style.visibility = "hidden";
            }
        }

        function ShowPanel() {
            el = document.getElementById('<%=
pnlPopupContainer.ClientID%>');
            if (el != null) {
                el.style.visibility = "visible";
            }

        }


Through this code you can view it in this way.



And you can customize it as per your requirement further.