Created
May 26, 2012 02:36
-
-
Save foofoodog/2791795 to your computer and use it in GitHub Desktop.
Super simple sharepoint list item ORM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.SharePoint; | |
using System.Reflection; | |
using System; | |
public class Contacts | |
{ | |
public int ID { get; set; } | |
public string Title { get; set; } | |
public string FirstName { get; set; } | |
public string Email { get; set; } | |
public string CellPhone { get; set; } | |
} | |
public class Tasks | |
{ | |
public int ID { get; set; } | |
public string Title { get; set; } | |
public DateTime Modified { get; set; } | |
public DateTime Created { get; set; } | |
public string Author { get; set; } | |
public string Editor { get; set; } | |
} | |
// Simple SP ORM. | |
public class SPorm<T> where T : new() | |
{ | |
// All mapped classes require an ID. | |
private const string ID = "ID"; | |
// The web we are optionaly handed. | |
private SPWeb _web = null; | |
// Local cache for T properties. | |
private List<PropertyInfo> Properties; | |
// Local cache for T property names. | |
private string[] PropertyNames; | |
// Default ctor, populates cache items. | |
public SPorm() | |
{ | |
Properties = typeof(T).GetProperties().ToList(); | |
PropertyNames = (from x in Properties select x.Name).ToArray(); | |
} | |
// Extra ctor, in case we are being handed an elevated web, or something. | |
public SPorm(SPWeb Web) | |
: this() | |
{ | |
_web = Web; | |
} | |
// Make a new list item from the type and save it. | |
public int Create(T t) | |
{ | |
SPListItem item = List.AddItem(); | |
Properties.Where(p => p.Name != ID).ToList().ForEach(p => item[p.Name] = p.GetValue(t, null)); | |
item.Update(); | |
return (int)item[ID]; | |
} | |
// Read all items. | |
public List<T> Read() | |
{ | |
return List.GetItems(PropertyNames).Cast<SPListItem>().Select(item => GetItem(item)).ToList(); | |
} | |
// Read an item. | |
public T Read(int Id) | |
{ | |
return GetItem(List.GetItemByIdSelectedFields(Id, PropertyNames)); | |
} | |
// Update an item. | |
public void Update(T t) | |
{ | |
var id = (int)Properties.Where(p => p.Name == ID).Single().GetValue(t, null); | |
SPListItem item = List.GetItemByIdSelectedFields(id, PropertyNames); | |
Properties.Where(p => p.Name != ID).ToList().ForEach(p => item[p.Name] = p.GetValue(t, null)); | |
item.Update(); | |
} | |
// Delete an item. | |
public void Delete(int Id) | |
{ | |
SPListItem item = List.GetItemById(Id); | |
item.Delete(); | |
} | |
// Use the web we were handed, or the one we can get at. | |
private SPWeb Web | |
{ | |
get | |
{ | |
return _web ?? SPContext.Current.Web; | |
} | |
} | |
// Get the list, upon request | |
private SPList List { | |
get { | |
return Web.Lists[typeof(T).Name]; | |
} | |
} | |
// Map a list item to a type. | |
private T GetItem(SPListItem item) | |
{ | |
var t = new T(); | |
Properties.ForEach(p => p.SetValue(t, item[p.Name], null)); | |
return t; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Microsoft.SharePoint; | |
namespace TestProject2 | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
SPSite site; | |
SPWeb web; | |
SPorm<Contacts> sporm; | |
int Id; | |
[TestInitialize] | |
public void Init() | |
{ | |
site = new SPSite("http://sp2010-1:2020"); | |
web = site.OpenWeb(); | |
sporm = new SPorm<Contacts>(web); | |
} | |
[TestCleanup] | |
public void Term() | |
{ | |
web.Dispose(); | |
site.Dispose(); | |
} | |
[TestMethod] | |
public void Test1() | |
{ | |
int count = sporm.Read().Count(); | |
Contacts c = new Contacts() { Title = "Fine", FirstName = "Larry" }; | |
Id = sporm.Create(c); | |
Assert.IsTrue(sporm.Read().Count() == count + 1); | |
c.ID = Id; | |
c.FirstName = "Leonard"; | |
sporm.Update(c); | |
var newc = sporm.Read(Id); | |
Assert.IsTrue(newc.FirstName == "Leonard"); | |
sporm.Delete(Id); | |
Assert.IsTrue(sporm.Read().Count() == count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment