private IHttpActionResult UpdateEntity <TEntity>(TEntity entity) where TEntity : ListItemEntity, new() { try { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var projectTaskContext = new ProjectTaskContext(context); if (entity.Id > 0) { var entry = projectTaskContext.List <TEntity>().WithPermissions().AddOrUpdate(entity); projectTaskContext.SaveChanges(); return(Json(entry.Entity)); } else { throw new Exception($"Cannot update the item with ID={entity.Id}."); } } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }
public IHttpActionResult Deploy() { try { using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { ProjectTaskContext projectTaskContext = new ProjectTaskContext(context); ProjectTaskProvisionModel <SpDataContext> projectTaskProvisionModel = projectTaskContext.CreateModel(); projectTaskProvisionModel.UnProvision(true); projectTaskProvisionModel.Provision(); List list = projectTaskContext.List <ProjectTask>().GetSpList(); Helper.SetAppInstalled(list, true); // Helper.SetAppInstalled(context, true); //using (ClientContext appContext = new Authentication.LowTrustTokenHelper(_tokenHelper).GetAppOnlyClienContext(context.Url)) //{ // Helper.SetAppInstalled(appContext, true); //} return(Json(new { ok = true })); } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }
public HttpResponseMessage GetUserPhoto(string accountName, string size = "M") { using (var client = new WebClient()) { //client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { string accessToken = Helper.GetAccessToken(context); if (!string.IsNullOrEmpty(accessToken)) { client.Headers.Add(HttpRequestHeader.Authorization, new AuthenticationHeaderValue("Bearer", accessToken).ToString()); } string pictureUrl = null; string userPhotoUrl = $"{context.Url.TrimEnd('/')}/_layouts/15/userphoto.aspx?accountname={System.Web.HttpUtility.UrlEncode(accountName)}&size={size}&url={System.Web.HttpUtility.UrlEncode(pictureUrl)}"; //$"{context.Url.TrimEnd('/')}/_vti_bin/DelveApi.ashx/people/profileimage?userId={System.Web.HttpUtility.UrlEncode(accountName)}&size={size}" var userPhoto = client.DownloadData(userPhotoUrl); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(userPhoto) //new StringContent($"data:image/png;base64,{Convert.ToBase64String(userPhoto)}") }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); return(response); } } }
public async Task <ActionResult> Index() { using (ClientContext userContext = new Authentication.LowTrustTokenHelper(_lowTrustTokenHelper).GetUserClientContext()) { using (ClientContext context = new Authentication.LowTrustTokenHelper(_lowTrustTokenHelper).GetAppOnlyClienContext(userContext.Url)) { // ViewBag.IsAppInstalled = await Helper.IsAppInstalled(context); // await LoadData(userContext); // return View(); List list = new ProjectTaskContext(context).List <ProjectTask>().GetSpList(); if (await Helper.IsAppInstalled(list)) { ViewBag.IsAppInstalled = true; await LoadData(userContext); return(View()); } else { await Task.FromResult(0); return(RedirectToAction("Admin")); } } } }
private IList <PrincipalInfo> GetUserPrincipals(string input, int limit) { using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var users = Utility.SearchPrincipals(context, context.Web, input, PrincipalType.User, PrincipalSource.UserInfoList, context.Web.SiteUsers, limit); context.ExecuteQuery(); return(users); } }
private UserCollection GetSiteUsers() { using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var users = context.Web.SiteUsers; context.Load(users, user => user.Include(u => u.Title, u => u.LoginName, u => u.Email, u => u.UserPrincipalName, u => u.Id)); context.ExecuteQuery(); return(users); } }
public async Task <ActionResult> Admin() { using (ClientContext context = new Authentication.LowTrustTokenHelper(_lowTrustTokenHelper).GetUserClientContext()) { await LoadData(context); List list = new ProjectTaskContext(context).List <ProjectTask>().GetSpList(); ViewBag.IsAppInstalled = await Helper.IsAppInstalled(list); return(View()); } }
private IHttpActionResult GetDataResult <TEntity>(string where, int count, string sortBy, string groupBy, string pagingToken, string[] fields = null) where TEntity : ListItemEntity, new() { try { using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var result = new DataResult <TEntity>(); result.Load(context, where, count, sortBy, groupBy, pagingToken, fields); return(Json(result)); } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }
private IHttpActionResult GetItemResult <TEntity>(int id) where TEntity : ListItemEntity, new() { try { using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var projectTaskContext = new ProjectTaskContext(context); var result = (id > 0) ? projectTaskContext.List <TEntity>().WithPermissions().FirstOrDefault((TEntity item) => item.Id == id) : null; if (result == null) { throw new Exception($"Item with Id={id} not found."); } return(Json(result)); } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }
private IHttpActionResult Delete <TEntity>(IEnumerable <int> itemIds) where TEntity : ListItemEntity, new() { try { if (itemIds == null) { throw new ArgumentNullException(nameof(itemIds)); } using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var projectTaskContext = new ProjectTaskContext(context); bool result = projectTaskContext.List <TEntity>().Delete(itemIds.ToArray()); projectTaskContext.SaveChanges(); return(Json(result)); } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }
private IHttpActionResult AddEntity <TEntity>(TEntity entity) where TEntity : ListItemEntity, new() { try { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } using (ClientContext context = new Authentication.LowTrustTokenHelper(_tokenHelper).GetUserClientContext()) { var projectTaskContext = new ProjectTaskContext(context); var entry = projectTaskContext.List <TEntity>().AddOrUpdate(entity, 0); projectTaskContext.SaveChanges(); return(Json(entry.Entity)); } } catch (Exception ex) { return(new JsonErrorResult(ex)); } }