1

I need to make query in SharePoint WebPart with VisualStudio. I have list "Employees" and need to check this list for existing UserName!

        <tr>
            <td class="auto-style1">User Name<span class="ms-error">*</span></td>
            <td>
                <asp:TextBox
                    ID="txtEmpUName"
                    runat="server">
                </asp:TextBox>
                <br />
            </td>
        </tr>



            //UserName Validation

            if (string.IsNullOrEmpty(txtEmpUName.Text.Trim()))
            {
                return "Fill the User Name!";
            }

            using (SPSite oSPsite = new SPSite("http://webapp/sites/siteCollection/"))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    SPList EmpList = oSPWeb.Lists["Employees"];

                    for (int i = 0; i < EmpList.Fields.Count; i++)
                    {
                        if (EmpList.Fields[i].Title == txtEmpUName.Text)
                        {
                            return "UserName exists";
                        }
                    }
                }
            }

How to check for existing Field in ListItem ? Thank you previously!

3 Answers 3

1

Try below code. From the screenshot it seems that Title field renamed to user name.

 using (SPSite oSPSite = new SPSite("http://webapp/sites/siteCollection/"))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb())
                    {
                      SPList empList = oSPWeb.Lists["Employees"];
                      SPQuery query = new SPQuery();
                      query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + txtEmpUName.Text.Trim() + "</Value></Eq></Where>";
                       SPListItemCollection olistitems = empList.GetItems(query);                            

                        if (olistitems.Count > 0)
                        {
                            return "UserName exists";
                        }
                    }
            }
2
  • So what's the difference between your answer and mine? Commented Mar 16, 2016 at 12:14
  • not much, i just used SPObjects
    – P S
    Commented Mar 16, 2016 at 12:27
1

If I had to do this, it would be something like this:

        using (SPSite oSPsite = new SPSite("http://webapp/sites/siteCollection/"))
        {
            using (SPWeb oSPWeb = oSPsite.OpenWeb())
            {
                var empList = oSPWeb.TryGetList("Employees");
                var getEmployeesQuery = new SPQuery();
                getEmployeesQuery.ViewXml = @"<Where>
                                               <Eq>
                                                 <FieldRef Name='Title'/>
                                                 <Value Type='Text'>"+txtEmpUName.Text.Trim()+"</Value>
                                               </Eq>
                                             </Where>";

                var employees = empList.GetItems(getEmployeesQuery);
                if(employees.Count > 0)
                {
                   return "UserName exists";
                }
            }
        }

Upon getting the results I am checking the count of results returned. If the count is zero, you will know that no item in your list has the same user name.

By the way, if you are looking to ensure that the usernames in your list are unique, I think you would be better off just by marking your field as unique from list settings (or from site columns under site settings).

3
  • Please Help me with SPQuery, Thank you previously!
    – Goshky
    Commented Mar 15, 2016 at 15:04
  • In any case it gives me that username exists.
    – Goshky
    Commented Mar 16, 2016 at 6:57
  • This is not works :(
    – Goshky
    Commented Mar 16, 2016 at 9:41
0
 using (SPSite oSPSite = new SPSite("http://webapp/sites/siteCollection/"))
                {
                    using (SPWeb oSPWeb = oSPSite.OpenWeb())
                    {
                        var empList = oSPWeb.Lists["Employees"];
                        var getEmployeesQuery = new SPQuery();

                        getEmployeesQuery.ViewXml =
                        @"<Where><Eq><FieldRef Name = 'User Name'/><Value Type='Text'>"+txtEmpUName.Text.Trim()+"</Value></Eq></Where>";

                        var employees = empList.GetItems(getEmployeesQuery);

                        if (employees.Count > 0)
                        {
                            return "UserName exists";
                        }
                    }
            }

After using query :

enter image description here

In any case it gives me that username exists.

With this :

                        if (employees.Count > 0)
                        {
                            return "UserName exists";
                        }
                        if (employees.Count == 0)
                        {
                            return "UserName not exists";
                        }

Again show me "UserName exists", maybe wrong query ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.