I'm new to developing sp projects , especially with multitier architecture, so I'm looking for some advices and suggestion how to optimize my code.I've got lot of "similar" queries that I need to perform , so my idea is to write method that will take query string as parametar and return SPListItemCollection
, something like this:
Private static SPListItemCollection GetPhoneNumbers(string queryString)
{
using (SPSite currentSite = new SPSite(Models.Sol.SiteName))
{
using (SPWeb currentWeb = currentSite.OpenWeb())
{
SPList myList = currentWeb.Lists[Models.Sol.PB1.ListName];
SPQuery query = new SPQuery();
query.Query = queryString;
return myList.GetItems(query);
}
}
}
Then I have CAMLQueryBuilder
class that I've created , and idea is to create static string field
for each query I need (strings will be passed to GetPhoneNumbers
method.
Also I have 2 static properties List<string>Office
getting list of offices
and string Department
.
I have 3 queries, first one should return all Employee numbers, second one should return all employee numbers from the employee office, and third employee numbers from employee office and department.Also , employee can work in 2 offices ,so that made this a little bit difficult.
I've created static
method for that purpose
public static string OfficeSearch(int n)
{
if (n == 1)
{
return "<Eq><FieldRef Name='" + Models.Office + "'/><Value Type='Choice'>" + Office[n-1] + "</Value></Eq>";
}
string s= "<Or>";
s += "<Eq><FieldRef Name='" + Models.Office + "'/><Value Type='Choice'>" + Office[n - 1] + "</Value></Eq>";
s += OfficeSearch(n - 1);
s += "</Or>";
return s;
}
And also I've created static field
for some code that is repeating
public static string EmployeeNum ="<IsNotNull><FieldRef Name='" + Models.EmployeeName + "'/></IsNotNull>";
First query, returning all employee numbers :
public static string AllEmployee = "<Where>" + EmployeeNum + "</Where>";
Second query, returning employee numbers from same Office :
public static string EmployeeOffice =
"<Where>" +
"<And>" +
EmployeeNum +
OfficeSearch() +
"</And>" +
"</Where>";
Third query , returning employee number from same office and department
public static string EmployeeOfficeDepartment =
"<Where>" +
"<And>" +
"<Eq><FieldRef Name='" + Models.Department + "'/><Value Type='Choice'>" +
Department + "</Value></Eq>" +
"<And>" +
OfficeSearch() +
EmployeeNum +
"</And>" +
"</And></Where>";
I believe that there are many different approaches that are more efficient , so please if you have any idea share it with me.Thank you.