Sharepoint Client Object model access from WCF (SOA) service.

Customers.svc

  <%@ ServiceHost Language=”C#” Debug=”true” Service=”MIA.Speech1.WcfService.Customers” CodeBehind=”Customers.svc.cs” %>

Customers.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using MIA.Speech1.Helpers;

namespace MIA.Speech1.WcfService
{
public class Customers : ICustomers
{

public List GetAllCustomers() {
return ClientObjectModelHelper.GetAllCustomers();

}

public Customer GetCustomerByID(string id) {
return ClientObjectModelHelper.GetCustomerByID(id);
}

public List CreateCustomer(string lastName, string firstName, string email, string address1) {
Customer c = new Customer();
c.Address1 = address1;
c.LastName = lastName;
c.FirstName = firstName;
c.EmailAddress = email;
return ClientObjectModelHelper.CreateCustomer(c);
}

public bool UpdateEmailAddress(string id, string emailAddress)
{
Customer c = new Customer();
c.idBDC = id;
c.EmailAddress = emailAddress;
return ClientObjectModelHelper.UpdateEmailAddress(c);
}

public bool UpdateName(string id, string lastName, string firstName)
{
Customer c = new Customer();
c.idBDC = id;
c.LastName = lastName;
c.FirstName = firstName;
return ClientObjectModelHelper.UpdateEmailAddress(c);
}
}
}

ICustomers.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using MIA.Speech1.Helpers;

namespace MIA.Speech1.WcfService
{

[ServiceContract]
public interface ICustomers
{
[OperationContract]
List GetAllCustomers();

[OperationContract]
Customer GetCustomerByID(string id);

[OperationContract]
List CreateCustomer(string lastName, string firstName, string email, string address1);

[OperationContract]
bool UpdateEmailAddress(string id, string emailAddress);

[OperationContract]
bool UpdateName(string id, string lastName, string firstName);
}

}

Customer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace MIA.Speech1.Helpers
{
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Customer
{
[DataMember]
public string idBDC { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string EmailAddress { get; set; }
[DataMember]
public string Address1 { get; set; }
}
}

ClientObject Helper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

namespace MIA.Speech1.Helpers
{
public static class ClientObjectModelHelper
{

public static List GetAllCustomers()
{
using (ClientContext spContext = new ClientContext(ConstantsHelper.SPSiteURI))
{
spContext.AuthenticationMode = ClientAuthenticationMode.Default;

// To Show a list of lists
Web web = spContext.Web;
spContext.Load(web);
spContext.Load(web.Lists);
spContext.ExecuteQuery();
ListCollection ls = web.Lists;
StringBuilder sb = new StringBuilder();
foreach(List eachList in web.Lists)
{
sb.Append(String.Format(“[{0}]\n”, eachList.Title));
}
string s = sb.ToString();

//Use CamlQuery to get just the item you want.
Microsoft.SharePoint.Client.List list = spContext.Web.Lists.GetByTitle(ConstantsHelper.CustomerListName);
ListItemCollection items = list.GetItems(new CamlQuery());
spContext.Load(items);
spContext.ExecuteQuery();
//A View
//query.ViewXml = String.Format(“{0}”,lastName);

//return (from a in items.AsEnumerable()
// select new Customer()
// {
// id = Int64.Parse(a.FieldValues[“id”].ToString()),
// EmailAddress = a.FieldValues[“EmailAddress”].ToString(),
// LastName = a.FieldValues[“LastName”].ToString(),
// FirstName = a.FieldValues[“FirstName”].ToString(),
// Address1 = a.FieldValues[“Address1”].ToString()
// }).ToList();
List lc = new List();
foreach(ListItem l in items)
{
Customer c = new Customer();
c.Address1 = l.FieldValues[“Address1”].ToString();
c.EmailAddress = l.FieldValues[“EmailAddress”].ToString();
c.FirstName = l.FieldValues[“FirstName”].ToString();
c.idBDC = l.FieldValues[“BdcIdentity”].ToString();
c.LastName = l.FieldValues[“LastName”].ToString();
lc.Add(c);
}
return lc;
}
}

public static Customer GetCustomerByID(string BDCIdentity)
{
using (ClientContext spContext = new ClientContext(ConstantsHelper.SPSiteURI))
{
Web web = spContext.Web;
spContext.Load(web);
spContext.Load(web.Lists);
spContext.ExecuteQuery();
Microsoft.SharePoint.Client.List list = spContext.Web.Lists.GetByTitle(ConstantsHelper.CustomerListName);
ListItemCollection items = list.GetItems(new CamlQuery());
spContext.Load(items);
spContext.ExecuteQuery();
Customer c = new Customer();
//LINQ doesnt seem to work properly.
//ListItem myCustomerItem = items.Where(b => b.FieldValues[“BdcIdentity”].ToString() == BDCIdentity).FirstOrDefault();
foreach (ListItem myCustomerItem in items)
{
if (myCustomerItem.FieldValues[“BdcIdentity”].ToString() == BDCIdentity)
{
c.idBDC = myCustomerItem.FieldValues[“BdcIdentity”].ToString();
c.EmailAddress = myCustomerItem.FieldValues[“EmailAddress”].ToString();
c.LastName = myCustomerItem.FieldValues[“LastName”].ToString();
c.FirstName = myCustomerItem.FieldValues[“FirstName”].ToString();
c.Address1 = myCustomerItem.FieldValues[“Address1”].ToString();
return c;
}
}
return c;
}
}

public static List CreateCustomer(Customer c)
{
using (ClientContext spContext = new ClientContext(ConstantsHelper.SPSiteURI))
{
Microsoft.SharePoint.Client.List list = spContext.Web.Lists.GetByTitle(ConstantsHelper.CustomerListName);
spContext.AuthenticationMode = ClientAuthenticationMode.Default;
spContext.ExecuteQuery();
ListItemCreationInformation newItem = new ListItemCreationInformation();
ListItem newCustomer = list.AddItem(newItem);
newCustomer[“Address1”] = c.Address1;
newCustomer[“EmailAddress”] = c.EmailAddress;
newCustomer[“LastName”] = c.LastName;
newCustomer[“FirstName”] = c.FirstName;
newCustomer.Update();
spContext.ExecuteQuery();
list.GetItems(new CamlQuery());
return GetAllCustomers();
}
}

public static bool UpdateEmailAddress(Customer c)
{
using (ClientContext spContext = new ClientContext(ConstantsHelper.SPSiteURI))
{
List list = spContext.Web.Lists.GetByTitle(ConstantsHelper.CustomerListName);
spContext.AuthenticationMode = ClientAuthenticationMode.Default;
CamlQuery query = CamlQuery.CreateAllItemsQuery(); ;
ListItemCollection listItems = list.GetItems(query);
// Should only take
spContext.Load(listItems, items => items.Where(a => a.FieldValues[“BdcIdentity”].ToString() == c.idBDC));
spContext.ExecuteQuery();
if (listItems.Count > 0)
{
//Should only be one returned.
listItems[0].FieldValues[“EmailAddress”] = c.EmailAddress;
listItems[0].Update();
spContext.ExecuteQuery();
listItems = list.GetItems(query);
spContext.Load(listItems, items => items.Where(a => a.FieldValues[“BdcIdentity”].ToString() == c.idBDC));
spContext.ExecuteQuery();
return listItems[0].FieldValues[“EmailAddress”].ToString() == c.EmailAddress;
}
return false;
}
}

public static bool UpdateName(Customer c)
{
using (ClientContext spContext = new ClientContext(ConstantsHelper.SPSiteURI))
{
List list = spContext.Web.Lists.GetByTitle(ConstantsHelper.CustomerListName);
spContext.AuthenticationMode = ClientAuthenticationMode.Default;
spContext.AuthenticationMode = ClientAuthenticationMode.Default;
CamlQuery query = CamlQuery.CreateAllItemsQuery(); ;
ListItemCollection listItems = list.GetItems(query);
// Should only take
spContext.Load(listItems, items => items.Where(a => a.FieldValues[“BdcIdentity”] == c.idBDC));
spContext.ExecuteQuery();
//Should only be one returned.
if (listItems.Count > 0)
{
listItems[0].FieldValues[“LastName”]= c.LastName;
listItems[0].FieldValues[“FirstName”] = c.FirstName;
listItems[0].Update();
spContext.ExecuteQuery();
listItems = list.GetItems(query);
spContext.Load(listItems, items => items.Where(a => a.FieldValues[“BdcIdentity”].ToString() == c.idBDC));
spContext.ExecuteQuery();
return listItems[0].FieldValues[“FirstName”].ToString() == c.FirstName && listItems[0].FieldValues[“LastName”].ToString() == c.LastName;
}
return false;
}
}
}
}



Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: