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!

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";
                        }
                    }
            }
  • So what's the difference between your answer and mine? – Nisarg Mar 16 '16 at 12:14
  • not much, i just used SPObjects – P S Mar 16 '16 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).

  • Please Help me with SPQuery, Thank you previously! – Goshky Mar 15 '16 at 15:04
  • In any case it gives me that username exists. – Goshky Mar 16 '16 at 6:57
  • This is not works :( – Goshky Mar 16 '16 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, privacy policy and cookie policy

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