Example #1
0
        public static IRestResponse Get(IntegrationManagerRequest request)
        {
            var response = new DefaultRestResponse
            {
                Name = "IntegrationManager"
            };

            try
            {
                RestIntegrationManager manager = null;
                bool hasManagerId = !String.IsNullOrEmpty(request.ManagerId);
                bool hasGroupId   = request.GroupId.HasValue;
                if (hasManagerId)
                {
                    manager = new RestIntegrationManager(IntegrationManagerPlugin.GetAllProviders().FirstOrDefault(m => m.Id == request.ManagerId));
                }
                else if (hasGroupId)
                {
                    manager = new RestIntegrationManager(IntegrationManagerPlugin.GetAllProviders().FirstOrDefault(m => m.TEGroupId == request.GroupId.Value));
                }
                response.Data = manager;
            }
            catch (Exception ex)
            {
                response.Errors = new[] { ex.Message };
            }
            return(response);
        }
        public IRestResponse Get(SPViewItemRequest request)
        {
            var response = new DefaultRestResponse
            {
                Name = "View"
            };

            var errors = new List <string>();

            ValidateUrl(request.Url, errors);
            var listId = ValidateListId(request.ListId, errors);
            var viewId = ValidateViewId(request.ViewId, errors);

            if (errors.Any())
            {
                response.Errors = errors.ToArray();
                return(response);
            }

            response.Data = cacheService.Get(CacheKey(listId, viewId, request.Url), CacheScope.Context | CacheScope.Process);
            if (response.Data == null)
            {
                try
                {
                    using (var clientContext = new SPContext(request.Url, IntegrationManagerPlugin.CurrentAuth(request.Url), runAsServiceAccount: true))
                    {
                        var site = clientContext.Site;
                        clientContext.Load(site, s => s.Id);

                        var web = clientContext.Web;
                        clientContext.Load(web, w => w.Id);

                        var list = clientContext.Web.Lists.GetById(listId);
                        var view = list.GetView(viewId);
                        clientContext.Load(view, RestSPView.InstanceQuery);

                        clientContext.ExecuteQuery();

                        response.Data = new SPViewItemData
                        {
                            Item   = new RestSPView(view),
                            SiteId = site.Id,
                            WebId  = web.Id
                        };
                    }
                    cacheService.Put(CacheKey(listId, viewId, request.Url), response.Data, CacheScope.Context | CacheScope.Process, new string[] { }, CacheTimeOut);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.Views.Get() method for ListId: {1} ViewId: {2} SPWebUrl: '{3}'. The exception message is: {4}",
                                                   ex.GetType(), listId, viewId, request.Url, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }
            return(response);
        }
Example #3
0
        public static IRestResponse List(IntegrationManagerListRequest request)
        {
            var response = new DefaultRestResponse
            {
                Name = "IntegrationManagerList"
            };

            try
            {
                List <IntegrationProvider> managerList = null;

                Func <IntegrationProvider, bool> filter = (m => true);
                bool hasSiteNameFilter  = !String.IsNullOrEmpty(request.SiteNameFilter);
                bool hasGroupNameFilter = !String.IsNullOrEmpty(request.GroupNameFilter);
                if (hasSiteNameFilter && hasGroupNameFilter)
                {
                    filter = (m => m.SPSiteName.Contains(request.SiteNameFilter, StringComparison.OrdinalIgnoreCase) || m.TEGroupName.Contains(request.GroupNameFilter, StringComparison.OrdinalIgnoreCase));
                }
                else if (hasSiteNameFilter)
                {
                    filter = (m => m.SPSiteName.Contains(request.SiteNameFilter, StringComparison.OrdinalIgnoreCase));
                }
                else if (hasGroupNameFilter)
                {
                    filter = (m => m.TEGroupName.Contains(request.GroupNameFilter, StringComparison.OrdinalIgnoreCase));
                }

                managerList   = IntegrationManagerPlugin.GetAllProviders().Where(filter).ToList();
                response.Data = new IntegrationManagerListData(managerList.Skip(request.PageIndex).Take(request.PageSize).Select(m => new RestIntegrationManager(m)), managerList.Count);
            }
            catch (Exception ex)
            {
                response.Errors = new[] { ex.Message };
            }
            return(response);
        }
        public IRestResponse List(SPViewCollectionRequest request)
        {
            var response = new DefaultRestResponse
            {
                Name = "Views"
            };

            var errors = new List <string>();

            ValidateUrl(request.Url, errors);
            var listId = ValidateListId(request.ListId, errors);

            if (errors.Any())
            {
                response.Errors = errors.ToArray();
                return(response);
            }

            string url            = request.Url;
            string viewNameFilter = request.ViewNameFilter;
            int    pageSize       = request.PageSize;
            int    pageIndex      = request.PageIndex;

            var viewCollection = (IEnumerable <View>)cacheService.Get(CacheKey(url, listId), CacheScope.Context | CacheScope.Process);

            if (viewCollection == null)
            {
                try
                {
                    using (var clientContext = new SPContext(url, IntegrationManagerPlugin.CurrentAuth(url), runAsServiceAccount: true))
                    {
                        var list = clientContext.Web.Lists.GetById(listId);
                        viewCollection = clientContext.LoadQuery(list.Views.Include(RestSPView.InstanceQuery));

                        clientContext.ExecuteQuery();
                    }
                    cacheService.Put(CacheKey(url, listId), viewCollection, CacheScope.Context | CacheScope.Process, new string[] { }, CacheTimeOut);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.View.List() method ListId:{1} SPWebUrl: '{2}'. The exception message is: {3}", ex.GetType(), listId, url, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }

            if (viewCollection != null)
            {
                try
                {
                    Func <View, bool> filter = (v => true);
                    bool hasViewNameFilter   = !String.IsNullOrEmpty(viewNameFilter);
                    if (hasViewNameFilter)
                    {
                        filter = v => v.Title.Contains(viewNameFilter, StringComparison.OrdinalIgnoreCase);
                    }

                    List <View> viewList = viewCollection.Where(filter).ToList();
                    response.Data = new SPViewCollectionData(viewList.Skip(pageIndex).Take(pageSize).Select(view => new RestSPView(view)), viewList.Count);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.View.List() method while processing not empty collection of Views for ListId: {1} SPWebUrl: '{2}'. The exception message is: {3}", ex.GetType(), listId, url, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }

            return(response);
        }
Example #5
0
        public IRestResponse List(SPListCollectionRequest request)
        {
            var response = new DefaultRestResponse
            {
                Name = "Lists"
            };

            var errors = new List <string>();

            ValidateUrl(request.Url, errors);
            if (errors.Any())
            {
                response.Errors = errors.ToArray();
                return(response);
            }

            string url            = request.Url;
            string includeType    = request.ListType;
            string excludeType    = request.ExcludeListType;
            string listNameFilter = request.ListNameFilter;
            int    pageSize       = request.PageSize;
            int    pageIndex      = request.PageIndex;

            var listCollection = (IEnumerable <List>)cacheService.Get(CacheKey(url, includeType, excludeType), CacheScope.Context | CacheScope.Process);

            if (listCollection == null)
            {
                try
                {
                    using (var clientContext = new SPContext(url, IntegrationManagerPlugin.CurrentAuth(url), runAsServiceAccount: true))
                    {
                        ListTemplateType includeLookUpTemplate;
                        ListTemplateType excludeLookUpTemplate;
                        var includeItemsByTemplate = TryGetTemplateType(includeType, out includeLookUpTemplate);
                        var excludeItemsByTemplate = TryGetTemplateType(excludeType, out excludeLookUpTemplate);
                        if (includeItemsByTemplate)
                        {
                            var lookUpTemplateValue = (int)includeLookUpTemplate;
                            listCollection = clientContext.LoadQuery(clientContext.Web.Lists.Where(list => list.BaseTemplate == lookUpTemplateValue && list.Hidden == false).Include(RestSPList.InstanceQuery));
                        }
                        else if (excludeItemsByTemplate)
                        {
                            var lookUpTemplateValue = (int)excludeLookUpTemplate;
                            listCollection = clientContext.LoadQuery(clientContext.Web.Lists.Where(list => list.BaseTemplate != lookUpTemplateValue && list.Hidden == false).Include(RestSPList.InstanceQuery));
                        }
                        else
                        {
                            listCollection = clientContext.LoadQuery(clientContext.Web.Lists.Include(RestSPList.InstanceQuery));
                        }

                        clientContext.ExecuteQuery();
                    }
                    cacheService.Put(CacheKey(url, includeType, excludeType), listCollection, CacheScope.Context | CacheScope.Process, new string[] { }, CacheTimeOut);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.Lists.List() method for SPWebUrl: '{1}'. The exception message is: {2}", ex.GetType(), url, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }

            if (listCollection != null)
            {
                try
                {
                    Func <List, bool> filter = (m => true);
                    bool hasListNameFilter   = !String.IsNullOrEmpty(listNameFilter);
                    if (hasListNameFilter)
                    {
                        filter = l => l.Title.StartsWith(listNameFilter, StringComparison.OrdinalIgnoreCase) || listNameFilter.Split(' ').Where(f => !string.IsNullOrWhiteSpace(f)).All(f => l.Title.Split(' ').Any(t => t.StartsWith(f, StringComparison.OrdinalIgnoreCase)));
                    }

                    List <List> itemsList = listCollection.Where(filter).ToList();
                    response.Data = new SPListCollectionData(itemsList.Skip(pageIndex).Take(pageSize).Select(item => new RestSPList(item)), itemsList.Count);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.Lists.List() method while processing not empty collection of Lists from SPWebUrl: '{1}'. The exception message is: {2}", ex.GetType(), url, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }
            return(response);
        }
Example #6
0
        public void Register(IRestEndpointController restRoutes)
        {
            // Retrieving user status by userId
            restRoutes.Add(2, "lync/userstatus/{userId}", HttpMethod.Get,
                           req =>
            {
                var response = new DefaultRestResponse {
                    Name = "UserStatus"
                };

                try
                {
                    int userId;
                    if (!Int32.TryParse(req.PathParameters["userId"] as string, out userId))
                    {
                        throw new ArgumentNullException("userId", "UserId is required");
                    }

                    var user = PublicApi.Users.Get(new UsersGetOptions {
                        Id = userId
                    });
                    if (user == null)
                    {
                        throw new ArgumentNullException("user", "User not found");
                    }

                    response.Data = new { status = _lyncCommunicationService.GetUserStatus(user).ToString(), userid = userId };
                    return(response);
                }
                catch (Exception ex)
                {
                    response.Errors = new[] { ex.Message };
                    return(response);
                }
            },
                           new RestEndpointDocumentation
            {
                EndpointDocumentation = new RestEndpointDocumentationAttribute
                {
                    Resource = "UserStatus",
                    Action   = "Get User Status"
                },
                RequestDocumentation = new List <RestRequestDocumentationAttribute>
                {
                    new RestRequestDocumentationAttribute
                    {
                        Name        = "UserId",
                        Type        = typeof(int),
                        Description = "Id of the user",
                        Required    = RestRequired.Required
                    }
                },
                ResponseDocumentation = new RestResponseDocumentationAttribute
                {
                    Name        = "UserStatus",
                    Description = "User Status. Can be one of the list - 'Away', 'BeRightBack', 'Busy', 'DoNotDisturb', 'IdleBusy', 'IdleOnline', 'Offwork', 'Online' or 'Offline'",
                    Type        = typeof(UserStatus)
                }
            });

            // Retrieving current user status
            restRoutes.Add(2, "lync/userstatus", HttpMethod.Get,
                           req =>
            {
                var response = new DefaultRestResponse {
                    Name = "UserStatus"
                };
                var user = PublicApi.Users.AccessingUser;

                try
                {
                    response.Data = new { status = _lyncCommunicationService.GetCurrentUserStatus().ToString(), userid = user.Id.GetValueOrDefault() };
                    return(response);
                }
                catch (Exception ex)
                {
                    response.Errors = new[] { ex.Message };
                    return(response);
                }
            },
                           new RestEndpointDocumentation
            {
                EndpointDocumentation = new RestEndpointDocumentationAttribute
                {
                    Resource = "UserStatus",
                    Action   = "Get Current User Status"
                },
                RequestDocumentation  = new List <RestRequestDocumentationAttribute>(),
                ResponseDocumentation = new RestResponseDocumentationAttribute
                {
                    Name        = "UserStatus",
                    Description = "User Status. Can be one of the list - 'Away', 'BeRightBack', 'Busy', 'DoNotDisturb', 'IdleBusy', 'IdleOnline', 'Offwork', 'Online' or 'Offline'",
                    Type        = typeof(UserStatus)
                }
            });

            // Updating current user status
            restRoutes.Add(2, "lync/userstatus", HttpMethod.Post,
                           req =>
            {
                var response = new DefaultRestResponse {
                    Name = "UserStatus"
                };
                var user = PublicApi.Users.AccessingUser;

                try
                {
                    PreferredUserStatus userStatus;
                    if (!Enum.TryParse(req.Form["userStatus"], out userStatus))
                    {
                        throw new ArgumentNullException("userStatus", "UserStatus not specified or has an invalid value. Allowed values - 'Away', 'BeRightBack', 'Busy', 'DoNotDisturb', 'Offwork', 'Online'");
                    }

                    _lyncCommunicationService.SetCurrentUserStatus(userStatus);

                    response.Data = new { status = _lyncCommunicationService.GetCurrentUserStatus().ToString(), userid = user.Id.GetValueOrDefault() };

                    return(response);
                }
                catch (Exception ex)
                {
                    response.Errors = new[] { ex.Message };
                    return(response);
                }
            },
                           new RestEndpointDocumentation
            {
                EndpointDocumentation = new RestEndpointDocumentationAttribute
                {
                    Resource = "UserStatus",
                    Action   = "Set Current User Status"
                },
                RequestDocumentation = new List <RestRequestDocumentationAttribute>
                {
                    new RestRequestDocumentationAttribute
                    {
                        Name        = "UserStatus",
                        Type        = typeof(PreferredUserStatus),
                        Description = "New status of the user. Allowed values - 'Away', 'BeRightBack', 'Busy', 'DoNotDisturb', 'Offwork', 'Online'",
                        Required    = RestRequired.Required
                    }
                },
                ResponseDocumentation = new RestResponseDocumentationAttribute
                {
                    Name        = "UserStatus",
                    Description = "User Status. Can be one of the list - 'Away', 'BeRightBack', 'Busy', 'DoNotDisturb', 'IdleBusy', 'IdleOnline', 'Offwork', 'Online' or 'Offline'",
                    Type        = typeof(UserStatus)
                }
            });
        }
        public IRestResponse List(SPUserOrGroupRequest request, bool onlyGroups = false, bool onlyUsers = false)
        {
            var response = new DefaultRestResponse
            {
                Name = "List"
            };

            var errors = new List <string>();

            ValidateUrl(request.Url, errors);
            if (errors.Any())
            {
                response.Errors = errors.ToArray();
                return(response);
            }

            string url       = request.Url;
            string search    = request.Search;
            int    pageSize  = request.PageSize > 0 ? request.PageSize : DefaultPageSize;
            int    pageIndex = request.PageIndex > 0 ? request.PageIndex : 0;

            response.Data = cacheService.Get(CacheKey(url, onlyGroups, onlyUsers, search, pageSize, pageIndex), CacheScope.Context | CacheScope.Process);
            if (response.Data == null)
            {
                try
                {
                    using (var clientContext = new SPContext(url, IntegrationManagerPlugin.CurrentAuth(url), runAsServiceAccount: true))
                    {
                        var web = clientContext.Web;
                        clientContext.Load(web, w => w.Id);

                        var site = clientContext.Site;
                        clientContext.Load(site, s => s.Id);

                        var query = CamlQuery.CreateAllItemsQuery(pageSize);
                        query.ViewXml = String.Concat("<View><Query>", ViewFieldsSection(RestSPUserOrGroup.ViewFields), WhereSection(search, onlyGroups, onlyUsers), "</Query></View>");

                        if (pageIndex > 0)
                        {
                            var tempListItems = web.SiteUserInfoList.GetItems(CamlQuery.CreateAllItemsQuery(pageSize * pageIndex));
                            clientContext.Load(tempListItems, itemsCollection => itemsCollection.ListItemCollectionPosition);
                            clientContext.ExecuteQuery();

                            query.ListItemCollectionPosition = tempListItems.ListItemCollectionPosition;
                        }

                        var siteUsersAndGroups = clientContext.LoadQuery(web.SiteUserInfoList.GetItems(query));
                        clientContext.ExecuteQuery();

                        response.Data = siteUsersAndGroups.Select(RestSPUserOrGroup.Get).ToArray();
                    }
                    cacheService.Put(CacheKey(url, onlyGroups, onlyUsers, search, pageSize, pageIndex), response.Data, CacheScope.Context | CacheScope.Process, new string[] { }, CacheTimeOut);
                }
                catch (Exception ex)
                {
                    string message = string.Format("An exception of type {0} occurred in the RESTApi.GroupsAndUsers.List() method for SPWebUrl: '{1}' Search: '{2}' OnlyGroups: {3} OnlyUsers: {4}. The exception message is: {5}", ex.GetType(), url, search, onlyGroups, onlyUsers, ex.Message);
                    SPLog.RoleOperationUnavailable(ex, message);

                    response.Errors = new[] { plugin.Translate(SharePointEndpoints.Translations.UnknownError) };
                }
            }
            return(response);
        }