Sharepoint CAML query for SPQuery.GetItems() [modified]
-
Hi We have discussions webpart customized to us and I want to get the number of discussoins from a particular user. My problem is when I pass the First Name and Last Name of the user (which is the displayname on the ffront end )in my query then I would able to get the correct count but if I pass the username then I am not able to the correct count from that user. Is there a way that I can say that get by username. Please have a look at my SPQuery code: SPQuery query = new SPQuery(); SPList splist = spWeb.Lists["Forum"]; query.Query = "<Where><Or><Eq><FieldRef Name='Author'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq><Eq><FieldRef Name='Editor'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq></Or></Where>"; query.ViewFields = ""; SPListItemCollection spListColl = splist.GetItems(query); int discussionsCount = spListColl.Count; Can any one give me any idea please. Thanks, Regards, Aruna
-- http://ashakthi.blogspot.com http://kids-articles.blogspot.com
modified on Wednesday, November 25, 2009 4:20 AM
-
Hi We have discussions webpart customized to us and I want to get the number of discussoins from a particular user. My problem is when I pass the First Name and Last Name of the user (which is the displayname on the ffront end )in my query then I would able to get the correct count but if I pass the username then I am not able to the correct count from that user. Is there a way that I can say that get by username. Please have a look at my SPQuery code: SPQuery query = new SPQuery(); SPList splist = spWeb.Lists["Forum"]; query.Query = "<Where><Or><Eq><FieldRef Name='Author'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq><Eq><FieldRef Name='Editor'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq></Or></Where>"; query.ViewFields = ""; SPListItemCollection spListColl = splist.GetItems(query); int discussionsCount = spListColl.Count; Can any one give me any idea please. Thanks, Regards, Aruna
-- http://ashakthi.blogspot.com http://kids-articles.blogspot.com
modified on Wednesday, November 25, 2009 4:20 AM
User the following code to get username. The username is usually in "domain\\username" format. SPWeb oWeb = SPContext.Current.Web; SPUser sUser = oWeb.CurrentUser; string sUserName = sUser.LoginName Use the following method to remove extra slashes. public string getUserName(string strLogin) { string str = ""; // Parse the string to check if domain name is present. int idx = strLogin.IndexOf('\\'); if (idx == -1) { idx = strLogin.IndexOf('@'); } string strDomain; string strName; if (idx != -1) { strDomain = strLogin.Substring(0, idx); strName = strLogin.Substring(idx + 1); } else { strDomain = Environment.MachineName; strName = strLogin; } return strName; } } Check if it is working for you with or with out removing extra slashes. If you are looking for help with SPQuery let me know. -Susheel Dakoju
-
Hi We have discussions webpart customized to us and I want to get the number of discussoins from a particular user. My problem is when I pass the First Name and Last Name of the user (which is the displayname on the ffront end )in my query then I would able to get the correct count but if I pass the username then I am not able to the correct count from that user. Is there a way that I can say that get by username. Please have a look at my SPQuery code: SPQuery query = new SPQuery(); SPList splist = spWeb.Lists["Forum"]; query.Query = "<Where><Or><Eq><FieldRef Name='Author'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq><Eq><FieldRef Name='Editor'/>" + "<Value Type='User'>" + spUsrDisplayName + "</Value>" + "</Eq></Or></Where>"; query.ViewFields = ""; SPListItemCollection spListColl = splist.GetItems(query); int discussionsCount = spListColl.Count; Can any one give me any idea please. Thanks, Regards, Aruna
-- http://ashakthi.blogspot.com http://kids-articles.blogspot.com
modified on Wednesday, November 25, 2009 4:20 AM
For instance, let us take a State List which holds all the states of India. States are grouped under different zones i.e. North, South, East & West. If I need to retrieve all the States for North zone then my SP Query would look somewhat like, SPQuery stateQuery = new SPQuery(); stateQuery.Query = "<Where><Eq><FieldRef Name=\"Zone\"/><Value Type=\"Text\">North</Value></Eq></Where>"; SPListItemCollection stateCol = StateList.GetItems(stateQuery); In the code above the StateCollection object gets populated with the States only related to North zone. You can then bind this collection to a GridView or DataList or any other similar controls to view the actual values. http://www.mindfiresolutions.com/Sharepoint-SPQuery-30.php[^]
Cheers, Eliza
-
User the following code to get username. The username is usually in "domain\\username" format. SPWeb oWeb = SPContext.Current.Web; SPUser sUser = oWeb.CurrentUser; string sUserName = sUser.LoginName Use the following method to remove extra slashes. public string getUserName(string strLogin) { string str = ""; // Parse the string to check if domain name is present. int idx = strLogin.IndexOf('\\'); if (idx == -1) { idx = strLogin.IndexOf('@'); } string strDomain; string strName; if (idx != -1) { strDomain = strLogin.Substring(0, idx); strName = strLogin.Substring(idx + 1); } else { strDomain = Environment.MachineName; strName = strLogin; } return strName; } } Check if it is working for you with or with out removing extra slashes. If you are looking for help with SPQuery let me know. -Susheel Dakoju
For instance, let us take a State List which holds all the states of India. States are grouped under different zones i.e. North, South, East & West. If I need to retrieve all the States for North zone then my SP Query would look somewhat like, SPQuery stateQuery = new SPQuery(); stateQuery.Query = "<Where><Eq><FieldRef Name=\"Zone\"/><Value Type=\"Text\">North</Value></Eq></Where>"; SPListItemCollection stateCol = StateList.GetItems(stateQuery); In the code above the StateCollection object gets populated with the States only related to North zone. You can then bind this collection to a GridView or DataList or any other similar controls to view the actual values. http://www.mindfiresolutions.com/Sharepoint-SPQuery-30.php[^]
Cheers, Eliza