public static T AddOrGetExistingDocumentLocationAndSave <T>(this OrganizationServiceContext context, Entity sharePointSite, Entity entity, string relativeUrl) where T : Entity, new() { var location = context.AddOrGetExistingDocumentLocation <T>(sharePointSite, entity, relativeUrl); if (location.EntityState == EntityState.Created || location.EntityState == EntityState.Changed) { context.SaveChanges(); // refresh the context and result entity context.ClearChanges(); location = context.CreateQuery(_sharepointdocumentlocation).FirstOrDefault(loc => loc.GetAttributeValue <Guid>(_sharepointdocumentlocationid) == location.Id) as T; } return(location); }