Monday, 30 June 2014

GetList Item Count WebServices

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.SharePoint;
using System.DirectoryServices;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class SPWebService : System.Web.Services.WebService
{
public SPWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string GetSPList()
{
int count;
using (SPSite site = new SPSite(“http://URL/”))
{
using (SPWeb web = site.OpenWeb())
{
SPList ServiceList = web.Lists["MyListName"];
if (ServiceList != null)
{
count = ServiceList.ItemCount;
}
else
{
count = 0;
}
}
}
return count.ToString();
}
//Get Role form Active Directory
[WebMethod]
public string GetRoles(string FQN) {
string EmailID = string.Empty;
DirectorySearcher UserSearch = new DirectorySearcher(“(mailNickName= ” + FQN + “)”);
SearchResult mYResult = UserSearch.FindOne();
if (mYResult == null)
{
return EmailID;
}
DirectoryEntry EmployeeName = mYResult.GetDirectoryEntry();
EmailID =Convert.ToString(EmployeeName.Properties["Title"].Value);
return EmailID;
}
[WebMethod]
public string DoesUserExists(string UserName, string GroupName)
{
string Exists = “True”;
UserGroup.UserGroup webService = new UserGroup.UserGroup();
NetworkCredential mycredentials = new NetworkCredential(“blackPoint”, “B@Point”, “scsdomhyd01?);
webService.Credentials = mycredentials;
XmlNode UserGroups = webService.GetGroupCollectionFromUser(UserName);
foreach (XmlNode node in UserGroups.FirstChild.ChildNodes )
{
string SPGroupName = node.Attributes["Name"].Value.ToLower();
if (GroupName.ToLower() == SPGroupName)
{
break;
}
else
{
Exists = “False”;
}
}
return Exists;
}
}
Why won’t InfoPath work with SharePoint Web Services? It comes down to the fact that InfoPath doesn’t recognize that some web services, such as Lists.asmx’s GetListItems requires strings (which InfoPath handles just fine) and XmlNodes (which InfoPath can’t seem to figure out). If you try to use this web service as a data connection, you’ll get error messages, such as “Root Element is Missing” or, if you try to add some XML code to one of the fields, you’ll get “Element of parameter query is missing or invalid.”
The solution? Create a Proxy Web Service that takes in strings representing the Xml code and create XmlNodes from them.
Here’s the code to create such as proxy:
[WebMethod]
public XmlNode GetListItems(string listName, string viewID, string query, string viewFields, string rowLimit, string queryOptions, string webID)
{
// Create an instance of the SharePoint Lists Web Service
ListsWebService.Lists listService = GetListsServiceClient();
// Create the XmlNodes from the given innerXml strings
XmlNode queryElement = CreateNode(“Query”, query);
XmlNode viewFieldsElement = CreateNode(“ViewFields”, viewFields);
XmlNode queryOptionsElement = CreateNode(“QueryOptions”, queryOptions);
// Execute the SharePoint Lists Web Service
return listService.GetListItems(listName, viewID, queryElement, viewFieldsElement, rowLimit, queryOptionsElement, webID);
}
private ListsWebService.Lists GetListsServiceClient()
{
ListsWebService.Lists listService = new ListsWebService.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = SPContext.Current.Web.Url + “/_vti_bin/Lists.asmx”;
return listService;
}
private XmlNode CreateNode(string name, string innerXml)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode element = xmlDoc.CreateElement(name);
element.InnerXml = innerXml;
return element;
}
Ref:http://msdn.microsoft.com/en-us/library/ms464040.aspx
[WebMethod]
public XmlDocument GetAuthorByAuthorID(string authorID)
{
XmlDocument doc = new XmlDocument();
DataSet authorsDataSet = new DataSet(“Authors”);
using (SqlConnection conn = new
SqlConnection(“server=localhost;uid=sa;pwd=thiru;database=Pubs”))
{
SqlDataAdapter adapter = new SqlDataAdapter(“Select au_id,
au_lname, au_fname,
phone from authors
Where au_id = ‘” +
authorID + “‘” ,conn);
adapter.Fill(authorsDataSet,”Author”);
}
doc.LoadXml(authorsDataSet.GetXml());
return doc;
}
[WebMethod]
public XmlDocument GetAllAuthors()
{
XmlDocument doc = new XmlDocument();
DataSet authorsDataSet = new DataSet(“Authors”);
using (SqlConnection conn = new
SqlConnection(“server=localhost;uid=sa;pwd=thiru;
database=Pubs”))
{
SqlDataAdapter adapter = new SqlDataAdapter(“Select au_id,
au_lname, au_fname,
phone from authors ” ,conn);
adapter.Fill(authorsDataSet,”Author”);
}
doc.LoadXml(authorsDataSet.GetXml());
return doc;
}