Basic Usage - Happi-cat/Untech.SharePoint GitHub Wiki
- Create and annotate entities
- Create data context
- Build config
- Context initialization and usage
- Build LINQ query
- Get, Add, Update, Delete item
- Create required enities.
- Your entities can inherit
Untech.SharePoint.Common.Models.Entity
class that already has few built-in SP fields.
- Your entities can inherit
- 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.- Full list of annotational attributes can be found here.
- Class-like mapping can be used alternatively.
- Create data context class
- Inherit it from
Untech.SharePoint.Client.Data.SpClientContext
for CSOM or inherit it fromUntech.SharePoint.Server.SpServerContext
for SSOM. Or you can inherit it fromUntech.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); }
}
}
- 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.
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));
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); }
}
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);