private void SyncListItems(ListBase list)
        {
            var itemsFromSharePoint = listItemService.GetItemsFromSharePoint(list);

            if (itemsFromSharePoint.Any())
            {
                listItemDataService.AddUpdate(itemsFromSharePoint);
            }
        }
        public static void Validate(this ListBase list)
        {
            if (list.GroupId < 0)
            {
                throw new InvalidOperationException("The group identified is invalid.");
            }

            if (list.Id == Guid.Empty)
            {
                throw new InvalidOperationException("Id cannot be empty.");
            }

            if (string.IsNullOrEmpty(list.SPWebUrl))
            {
                throw new InvalidOperationException("Url cannot be empty.");
            }
        }
        public void Add(ListBase list)
        {
            Validate(list);

            if (string.IsNullOrEmpty(list.ApplicationKey))
            {
                var splist = Get(new ListGetQuery(list.Id, list.TypeId)
                {
                    Url = list.SPWebUrl
                });
                list.ApplicationKey = splist.Title;
            }

            listDataService.AddUpdate(list);

            SyncListItems(list);
        }
Ejemplo n.º 4
0
        public void AddUpdate(ListBase list)
        {
            list.Validate();

            // Make ApplicationKey valid and unique
            if (!string.IsNullOrEmpty(list.ApplicationKey))
            {
                int groupId = list.GroupId;
                list.ApplicationKey = applicationKeyValidator.MakeValid(list.ApplicationKey.ToLowerInvariant(), applicationKey =>
                {
                    // List is duplicate when there is another list with the same application key but different Id
                    var anotherList = Get(applicationKey, groupId);
                    return(anotherList != null && anotherList.Id != Guid.Empty && anotherList.Id != list.Id);
                });
            }

            try
            {
                using (var connection = DataHelpers.GetSqlConnection())
                {
                    using (var command = DataHelpers.CreateSprocCommand("[te_SharePoint_List_AddUpdate]", connection))
                    {
                        command.Parameters.Add("@ApplicationId", SqlDbType.UniqueIdentifier).Value = list.Id;
                        command.Parameters.Add("@ApplicationKey", SqlDbType.NVarChar, 256).Value   = list.ApplicationKey;
                        command.Parameters.Add("@TypeId", SqlDbType.UniqueIdentifier).Value        = list.TypeId;
                        command.Parameters.Add("@GroupId", SqlDbType.Int).Value             = list.GroupId;
                        command.Parameters.Add("@SPWebUrl", SqlDbType.NVarChar, 256).Value  = list.SPWebUrl;
                        command.Parameters.Add("@ViewId", SqlDbType.UniqueIdentifier).Value = list.ViewId;

                        connection.Open();
                        command.ExecuteNonQuery();
                        connection.Close();
                    }
                }

                // Clear Cache
                cacheService.RemoveByTags(new[] { GetTagId(list.Id) }, CacheScope.Context | CacheScope.Process);
            }
            catch (Exception ex)
            {
                string message = string.Format("An exception of type {0} occurred in the SPListDataService.AddUpdate() method. The exception message is: {1}", ex.GetType(), ex.Message);
                SPLog.DataProvider(ex, message);
                throw new AddLibraryException(ex.Message, list.SPWebUrl, list.Id, list.GroupId);
            }
        }
Ejemplo n.º 5
0
 private void PutInCache(ListBase listBase)
 {
     cacheService.Put(GetCacheId(listBase.GroupId, listBase.ApplicationKey), listBase, CacheScope.Context | CacheScope.Process, new[] { GetTagId(listBase.Id) }, CacheTimeOut);
     cacheService.Put(GetCacheId(listBase.Id), listBase, CacheScope.Context | CacheScope.Process, new[] { GetTagId(listBase.Id) }, CacheTimeOut);
 }
        public SPList Get(ListGetQuery options)
        {
            var list = new SPList();

            if (options.Id == Guid.Empty)
            {
                return(list);
            }

            ListBase listBase = null;

            string spwebUrl = options.Url;
            int    groupId  = options.GroupId;

            if (string.IsNullOrEmpty(spwebUrl))
            {
                try
                {
                    listBase = listDataService.Get(options.Id);
                    if (listBase == null)
                    {
                        return(null);
                    }

                    Validate(listBase);

                    spwebUrl = listBase.SPWebUrl;
                    groupId  = listBase.GroupId;
                }
                catch (InvalidOperationException)
                {
                    return(new SPList
                    {
                        ApplicationId = options.Id,
                        GroupId = options.GroupId,
                        SPWebUrl = options.Url
                    });
                }
            }

            try
            {
                using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                {
                    var site = clientContext.Site;
                    clientContext.Load(site, s => s.Id);

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

                    var spList = clientContext.Web.Lists.GetById(options.Id);
                    clientContext.Load(spList, NoHiddenFieldsInstanceQuery);
                    clientContext.ExecuteQuery();

                    list = new SPList(spList, site.Id)
                    {
                        GroupId = groupId,
                        ViewId  = listBase != null ? listBase.ViewId : Guid.Empty
                    };
                }
            }
            catch (ServerUnauthorizedAccessException)
            {
                return(null);
            }
            catch (Exception ex)
            {
                if (ex.IsUnauthorizedAccessException())
                {
                    return(null);
                }

                if (listBase != null && ex.Message.StartsWith("List does not exist."))
                {
                    Delete(listBase.Id, false);
                    list = new SPList {
                        Id = listBase.Id, GroupId = listBase.GroupId, SPWebUrl = listBase.SPWebUrl
                    };
                }
                else
                {
                    var message = string.Format("An exception of type {0} occurred in the InternalApi.SPListService.Get() method for ContentId: {1}. The exception message is: {2}",
                                                ex.GetType(),
                                                options.Id,
                                                ex.Message);

                    SPLog.RoleOperationUnavailable(ex, message);

                    throw new SPInternalException(message, ex);
                }
            }

            return(list);
        }
        public void Update(ListUpdateQuery options)
        {
            if (options.Id == Guid.Empty)
            {
                return;
            }

            ListBase listBase = null;
            string   spwebUrl = options.SPWebUrl;

            if (string.IsNullOrEmpty(spwebUrl))
            {
                listBase = listDataService.Get(options.Id);
                if (listBase == null)
                {
                    return;
                }

                Validate(listBase);
                spwebUrl = listBase.SPWebUrl;
            }

            try
            {
                var hasSharePointUpdates = options.Title != null || options.Description != null;
                if (hasSharePointUpdates)
                {
                    using (var clientContext = new SPContext(spwebUrl, credentials.Get(spwebUrl)))
                    {
                        var splist = clientContext.Web.Lists.GetById(options.Id);
                        if (!string.IsNullOrEmpty(options.Title))
                        {
                            splist.Title = options.Title;
                        }
                        if (options.Description != null)
                        {
                            splist.Description = options.Description;
                        }
                        splist.Update();
                        clientContext.ExecuteQuery();

                        if (listBase != null)
                        {
                            listBase.ApplicationKey = splist.Title;
                        }
                    }
                }
                if (listBase != null)
                {
                    listBase.ViewId = options.DefaultViewId ?? Guid.Empty;
                    listDataService.AddUpdate(listBase);
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("An exception of type {0} occurred in the InternalApi.SPListService.Update() method. The exception message is: {1}", ex.GetType(), ex.Message);
                SPLog.RoleOperationUnavailable(ex, message);

                throw new SPInternalException(message, ex);
            }
        }