Install latest version of NuGet package:
PM> Install-Package AE.SharePoint.ListsContextCore
Additionaly you can install NuGet package for registration in Microsoft DI container:
PM> Install-Package AE.SharePoint.ListsContextCore.Extensions.Microsoft.DependencyInjection
Create class with properties.
class ArticleListItem
{
public int Id { get; set; }
public string Title { get; set; }
}Supported types:
- int
- long
- double
- decimal
- bool
- DateTime
- string
- SharePoint Lookup
- SharePoint URL
Property mapped to SharePoint field by name. if you want to use property name different from SharePoint field name, use SharePointFieldNameAttribute.
class ArticleListItem
{
public int Id { get; set; }
public string Title { get; set; }
[SharePointFieldName("Created")]
public DateTime PublicationDate { get; set; }
}Use SharePointNotMappedAttribute for properties that do not need to be initialized with values from a SharePoint list.
class ArticleListItem
{
public int Id { get; set; }
public string Title { get; set; }
[SharePointFieldName("Created")]
public DateTime PublicationDate { get; set; }
[SharePointNotMapped]
public string Year { get; set; }
}Use SharePointLookupIdAttribute if you want to get SharePoint Lookup Id value. Use SharePointLookupValueAttribute if you want to get SharePoint Lookup value.
class ArticleListItem: IListItemBase
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[SharePointFieldName("Created")]
public DateTime PublicationDate { get; set; }
[SharePointNotMapped]
public string Year { get; set; }
[SharePointLookupId]
public int AuthorId { get; set; }
[SharePointLookupValue("Name")]
public int AuthorName { get; set; }
}Use SharePointUrlField class for SharePoint URL field type.
class AuthorListItem: ListItemBase
{
public string Name { get; set; }
SharePointUrlField Photo { get; set; }
}The class must inherit from SharePointListsContext. Add properties representing SharePointLists, use SharePointList generic type for that. Add to each property SharePointListNameAttribute with current name of SharePoint list.
class ExampleContext: SharePointListsContext
{
public ExampleContext(HttpClient client): base(client)
{
}
[SharePointListName("ArticlesList")]
public SharePointList<ArticleListItem> Articles { get; set; }
}Use ContextOptions to specify source and format for DateTime field. By default DateTime returns in UTC.
class ExampleContext: SharePointListsContext
{
public ExampleContext(HttpClient client) :
base(
client,
new ContextOptions
{
DatesFromText = true,
DatesFromTextFormat = "dd.MM.YYYY hh:mm"
}
)
{
}
[SharePointListName("ArticlesList")]
public SharePointList<ArticleListItem> Articles { get; set; }
}Register context by using AddSharePointListsContext extension method from AE.SharePoint.ListsContextCore.Extensions.Microsoft.DependencyInjection namespace. Set SharePointSiteUrl to SharePoint site address. Notise that trailing slash at the end of url. You may use Network, or SharePoint credentials to access the SharePoint site, or leave Credentials parameter not set if default (for example application pool credentials) are used.
serviceCollection.AddSharePointListsContext<ExampleContext>(options =>
{
options.SharePointSiteUrl = "http://sharepointsite.url/sites/test-site/";
// options.Credentials = new NetworkCredential(userName, password);
});You can get all items.
var context = serviceProvider.GetService<ExampleContext>();
List<ArticleListItem> items = await context.Articles.GetAllItemsAsync();Or get particular item by Id.
var context = serviceProvider.GetService<ExampleContext>();
ArticleListItem item = await context.Articles.GetItemAsync(1);Or get items from the list based on the specified CAML query.
string ViewXml = "<View>" +
"<Query>" +
"<Where><Eq>" +
"<FieldRef Name='Title' />" +
"<Value Type='Text'>Happy New Year</Value>" +
"</Eq></Where>" +
"</Query>" +
"</View>";
List<ArticleListItem> selectedItems = await context.Articles.GetItemsAsync(ViewXml);To add new item, create item class with specified properties and add it to list using AddItemAsync(T item) method. Method returns created item from SharePoint list.
var context = serviceProvider.GetService<ExampleContext>();
var newItem = new ArticleListItem
{
Title = "Happy New 2019 Year"
};
ArticleListItem createdItem = await context.Articles.AddItemAsync(newItem);If you want to update existing item use UpdateItemAsync(T item). Item class must implements IListItemBase interface. And have initialized Id property with identifier of item witch you want to update. You may use ListItemBase class as base class of your models.
var context = serviceProvider.GetService<ExampleContext>();
class ArticleListItem: ListItemBase
{
public string Title { get; set; }
[SharePointFieldName("Created")]
public DateTime PublicationDate { get; set; }
[SharePointNotMapped]
public string Year { get; set; }
}
createdItem.Title = "Happy New 2020 Year";
await context.Articles.UpdateItemAsync(newItem);To delete item from SharePoint list use DeleteItemAsync(int id) method.
var context = serviceProvider.GetService<ExampleContext>();
await context.Articles.DeleteItemAsync(1);You can limit quantity of returned elements by using Take method. Or you can receive only nessesary fields by using IncludeFields and ExcludeFields methods. In example below GetAllItemsAsync returns only 5 items, and not retrieve data for Content field, that field will be initialized with default value.
List<ArticleListItem> selectedItems = await context.Articles
.ExcludeFields(x => new { x.Content })
.Take(5)
.GetAllItemsAsync();- Created methods for getting intems from SharePoint List: Task<List> GetAllItemsAsync(), Task GetItemAsync(int id), Task<List> GetItemsAsync(string query)
- Created method for adding item to SharePoint List: Task AddItemAsync(T item)
- Created method for updating item in SharePoint List: Task UpdateItemAsync(T item)
- Created method for deleting item from SharePoint List: Task DeleteItemAsync(int id)
- Created method for limiting retrieved items SharePointList Take(int count)
- Created methods for limiting retrieved fields SharePointList IncludeFields(Expression<Func<T,object>> fields), SharePointList ExcludeFields(Expression<Func<T, object>> fields)
- Added ContextOptions.
- Added attributes for lookup: SharePointLookupIdAttribute and SharePointLookupValueAttribute.
- Added SharePointUrlField class.