Basic Usage - Happi-cat/Untech.SharePoint GitHub Wiki

Contents

  1. Create and annotate entities
  2. Create data context
  3. Build config
  4. Context initialization and usage
  5. Build LINQ query
  6. Get, Add, Update, Delete item

Create and annotate entities

  • Create required enities.
    • Your entities can inherit Untech.SharePoint.Common.Models.Entity class that already has few built-in SP fields.
  • Add Untech.SharePoint.Common library as a reference.
  • Add namespace usage: using Untech.SharePoint.Common.Mappings.Annotation;.
  • Annotate enities and their members with SpField attributes.

Back to Top

Create data context

  • Create data context class
  • Inherit it from Untech.SharePoint.Client.Data.SpClientContext for CSOM or inherit it from Untech.SharePoint.Server.SpServerContext for SSOM. Or you can inherit it from Untech.SharePoint.Common.Data.SpContext in order to easily reuse data context with SSOM, CSOM.
  • Add properties that will return Untech.SharePoint.Common.Data.ISpList<T>. T represents class that will be associated with the specified list content type.
  • Add getter to list properties: return GetList(ctx => ThisProperty);.
  • Annotate list properties with SpList attribute.

Example:

public class DataContext : SpContext<DataContext>
{
	public DataContext(ICommonService commonService)
		: base(commonService) { }

	[SpList("/Lists/News")]
	public ISpList<NewsModel> News 
	{
		get { return GetList(x => x.News); }
	}

	[SpList("/Lists/Events")]
	public ISpList<EventModel> Events
	{
		get { return GetList(x => x.Events); }
	}

	[SpList("/Lists/Teams")]
	public ISpList<TeamModel> Teams
	{
		get { return GetList(x => x.Teams); }
	}

	[SpList("/Lists/Projects")]
	public ISpList<ProjectModel> Projects
	{
		get { return GetList(x => x.Projects); }
	}
}

Back to Top

Build config

  • Initialize config builder.
	var cfgBuilder = Untech.SharePoint.Client.Config.ClientConfig.Begin(); // CSOM
	var cfgBuidler = Untech.SharePoint.Server.Config.ServerConfig.Begin(); // SSOM

NOTE: Configuration that was built with ClientConfig cannot be used with SSOM as well as ServerConfig cannot be used with CSOM.

  • ClientConfig - registers common & client specific field converters.
  • ServerConfig - registers common & server specific field converters.
  • Register all data contexts and specify their mappings sources:
	cfgBuilder.RegisterMappings(n => n.Annotated<WebDataContext>());
  • Complete configuration: var cfg = cfgBuilder.Build();

NOTE: Configuration instance can be made as a singleton.

Back to Top

Context initialization and usage

Example:

	// CSOM
    var clientCtx = new ClientContext("http://spserver/sites/some");
    var ctx = new DataContext(new SpClientCommonService(clientCtx, clientCfg));
	
	// SSOM
	var web = new SPSite("http://localhost/sites/some").OpenWeb();
    var ctx = new DataContext(new SpServerCommonService(web, serverCfg));

Back to Top

Build LINQ Query

ISpList inherits IQueryable so you can query items like in example below:

	var result = ctx.TestList
        .Where(n => n.SomeField.StartsWith("TTT") && n.SomeField2 == "Approved")
        .Where(n => n.Title.Contains("LALA"))
        .ToList();

NOTE: Each Linq-query will add equality-filter by default if it's not an external list. That additional filter will be represented in CAML-query by equality with content type id. There also should be mentioned that this behaviour can be changed during GetList call in data context.

public ISpList<Entity> Entities {
       	get { return GetList(c => c.Entities, SpListOptions.NoFilteringByContentType); } 
}

Back to Top

Get, Add, Update, Delete item

Usage of these methods will be identical and will have common limitations. They can be used with non-external SP lists only and entity type should have mapped Id field.

	ctx.TestList.Update(item);

Back to Top

⚠️ **GitHub.com Fallback** ⚠️