// in this case we're not using the spweb in the base class in fact we're passing it as a parameter
        public void CreateList(SPWeb currentWebSite)
        {
            SPWeb website = null;

            try
            {
                website = currentWebSite ?? GetWebSite();

                website.AllowUnsafeUpdates = true;

                SPListCollection lists  = website.Lists;
                Guid             idList = lists.Add(ListName, string.Empty, SPListTemplateType.GenericList);

                SPList list = lists.GetList(idList, false);
                list.NoCrawl = true;
                list.Update();

                website.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "CreateList", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
 public void DeleteUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return;
     }
     try
     {
         SPUser user = GetUserObject(username);
         if (user == null)
         {
             return;
         }
         using (SPWeb website = GetWebSite())
         {
             SPUserCollection varUsers = website.SiteUsers;
             varUsers.Remove(username);
             website.Update();
         }
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteUser", "ClsHelper"));
         log.WriteToLog();
     }
 }
        /// <summary>
        /// search an item in sharepoint list (don't forget to declare the constructor)
        /// </summary>
        /// <param name="query">the spquery string</param>
        /// <returns>return the item found</returns>
        public SPListItem SearchItem(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                return(null);
            }
            SPListItem item = null;

            try
            {
                if (GetItemCollection(query) != null)
                {
                    if (GetItemCollection(query).Count > 0)
                    {
                        item = GetItemCollection(query)[0];
                    }
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "SearchItem", "ClsHelper"));
                log.WriteToLog();
            }

            return(item);
        }
        public void DeleteList()
        {
            SPWeb website = null;

            try
            {
                website = GetWebSite();
                SPListCollection lists    = website.Lists;
                SPList           list     = lists[ListName];
                Guid             listGuid = list.ID;
                lists.Delete(listGuid);
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteList", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
Exemplo n.º 5
0
        public SPWeb GetWebSite()
        {
            if (!IsWebProcessValid())
            {
                return(null);
            }
            SPWeb site = null;

            try
            {
                using (SPSite siteCollection = GetSiteCollection())
                {
                    if (siteCollection != null)
                    {
                        site = siteCollection.AllWebs[SiteName];
                    }
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetWebSite", "ClsHelperParent"));
                log.WriteToLog();
            }
            finally
            {
                if (site != null)
                {
                    site.Dispose();
                }
            }

            return(site);
        }
Exemplo n.º 6
0
        public SPListItemCollection GetItemCollection()
        {
            SPListItemCollection items = null;

            try
            {
                if (GetList() == null)
                {
                    return(null);
                }

                SPList list = GetList();

                if (list != null)
                {
                    items = list.Items;
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetItemCollection", "ClsHelperParent"));
                log.WriteToLog();
            }

            return(items);
        }
        /// <summary>
        /// delete group of items in sharepoint
        /// </summary>
        /// <param name="items">the collection of items</param>
        public void DeleteItems(SPListItemCollection items)
        {
            if (items == null)
            {
                return;
            }

            try
            {
                int length = items.Count;

                for (int i = 0; i < length; i++)
                {
                    if (i == length - 1)
                    {
                        i = 0;
                    }

                    DeleteItem(items[i]);
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteItems", "ClsHelper"));
                log.WriteToLog();
            }
        }
Exemplo n.º 8
0
        public SPList GetList()
        {
            if (!IsListProcessValid())
            {
                return(null);
            }

            SPList list = null;

            try
            {
                using (SPWeb site = GetWebSite())
                {
                    if (site != null)
                    {
                        list = site.Lists[ListName];
                    }
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetList", "ClsHelperParent"));
                log.WriteToLog();
            }

            return(list);
        }
Exemplo n.º 9
0
        public UserProfileManager GetProfileManager()
        {
            UserProfileManager profileManager = null;

            try
            {
                using (SPSite siteCollection = GetSiteCollection())
                {
                    if (siteCollection == null)
                    {
                        return(null);
                    }

                    var context = SPServiceContext.GetContext(siteCollection);
                    profileManager = new UserProfileManager(context);
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetProfileManager", "ClsHelperParent"));
                log.WriteToLog();
            }

            return(profileManager);
        }
        public void ChangeGroupName(string oldGroupName, string newGroupName)
        {
            if (string.IsNullOrEmpty(oldGroupName))
            {
                return;
            }
            if (string.IsNullOrEmpty(newGroupName))
            {
                return;
            }

            try
            {
                SPGroup sharepointGroup = GetGroupObject(oldGroupName);
                if (sharepointGroup == null)
                {
                    return;
                }
                sharepointGroup.Name = newGroupName;
                sharepointGroup.Update();
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "aGroupName", "ClsHelper"));
                log.WriteToLog();
            }
        }
        public void CreateListView(string viewName, string query, StringCollection collViewFields, uint rowLimit,
                                   bool paged, bool makeViewDefault, SPViewCollection.SPViewType viewType, bool personalView)
        {
            SPWeb website = null;

            try
            {
                website = GetWebSite();
                SPListCollection lists = website.Lists;
                SPList           list  = lists[ListName];

                list.Views.Add(viewName, collViewFields, query, rowLimit, paged, makeViewDefault, viewType, personalView);

                SPView view = list.Views[viewName];

                view.Update();
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "CreateListView", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
        public void CreateSite(string siteName)
        {
            if (string.IsNullOrEmpty(siteName))
            {
                return;
            }

            SPWeb website = null;

            try
            {
                website = GetWebSite();
                string template = website.WebTemplate;
                website.Webs.Add(siteName, siteName, string.Empty, 0, template, false, false);
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "CreateSite", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
        public void AddUserToGroup(UserObject user)
        {
            if (user == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(user.UserName))
            {
                return;
            }
            if (string.IsNullOrEmpty(user.GroupName))
            {
                return;
            }

            try
            {
                SPUser sharepointUser = GetUserObject(user.UserName) ?? InsertUser(user);

                SPGroup sharepointGroup = GetGroupObject(user.GroupName);
                if (sharepointGroup == null)
                {
                    return;
                }

                sharepointGroup.AddUser(sharepointUser);
                sharepointGroup.Update();
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "AddUserToGroup", "ClsHelper"));
                log.WriteToLog();
            }
        }
        public void CreateFolder(string folderName)
        {
            if (string.IsNullOrEmpty(folderName))
            {
                return;
            }

            SPWeb website = null;

            try
            {
                website = GetWebSite();
                var library = (SPDocumentLibrary)website.Lists[ListName];
                SPFolderCollection folders = website.Folders;
                folders.Add(Utilities.RemoveSlashAtTheEnd(SiteUrl) + "/" + Utilities.RemoveSlashAtTheEnd(SiteName) + "/" + Utilities.RemoveSlashAtTheEnd(ListName) + "/" + Utilities.RemoveSlashAtTheEnd(folderName) + "/");
                library.Update();
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "CreateFolder", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
Exemplo n.º 15
0
        public SPSite GetSiteCollection()
        {
            if (!IsSiteCollectionProcessValid())
            {
                return(null);
            }
            SPSite siteCollection = null;

            try
            {
                siteCollection = new SPSite(SiteUrl);
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetSiteCollection", "ClsHelperParent"));
                log.WriteToLog();
            }
            finally
            {
                if (siteCollection != null)
                {
                    siteCollection.Dispose();
                }
            }

            return(siteCollection);
        }
        public SPUser InsertUser(UserObject user)
        {
            if (user == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(user.UserName))
            {
                return(null);
            }

            SPUser sharepointUser = null;

            try
            {
                using (SPWeb varWebSite = GetWebSite())
                {
                    SPUserCollection users = varWebSite.SiteUsers;
                    users.Add(user.UserName, user.Email, user.Name, user.Notes);
                    AssignPermission(user.Role, string.Empty, user.UserName);
                    sharepointUser = varWebSite.AllUsers[user.UserName];
                    varWebSite.Update();
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "InsertUser", "ClsHelper"));
                log.WriteToLog();
            }

            return(sharepointUser);
        }
        public UserProfile GetFarmUserProfile(string userAccount)
        {
            if (string.IsNullOrEmpty(userAccount))
            {
                return(null);
            }
            UserProfile user = null;

            try
            {
                UserProfileManager profileManager = GetProfileManager();

                if (profileManager.UserExists(userAccount))
                {
                    user = profileManager.GetUserProfile(userAccount);
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetUserProfile", "ClsHelper"));
                log.WriteToLog();
            }

            return(user);
        }
        public bool IsListExist()
        {
            SPWeb website = null;

            try
            {
                website = GetWebSite();

                if (website.Lists.Cast <SPList>().Any(list => list.Title == ListName))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "IsListExist", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (website != null)
                {
                    website.Dispose();
                }
            }

            return(false);
        }
        /// <summary>
        /// add new items to sharepoint lists (don't forget to declare the constructor)
        /// </summary>
        /// <param name="fields"> hash table that contains the item values (key: the column name Value: the value inside the column)</param>
        public void AddItemToList(Hashtable fields)
        {
            if (!IsHashtableValid(fields))
            {
                return;
            }
            try
            {
                SPListItemCollection itemCollection = GetItemCollection();
                if (itemCollection == null)
                {
                    return;
                }

                SPListItem  item = itemCollection.Add();
                ICollection keys = fields.Keys;
                FieldObject field;
                foreach (object key in keys)
                {
                    field = (FieldObject)fields[key];

                    switch (field.ValueType)
                    {
                    case FieldObject.Types.Text:
                        item[key.ToString()] = Convert.ToString(field.Value);
                        item.UpdateOverwriteVersion();
                        break;

                    case FieldObject.Types.Integer:
                        item[key.ToString()] = Convert.ToInt32(field.Value);
                        item.UpdateOverwriteVersion();
                        break;

                    case FieldObject.Types.DateTime:
                        item[key.ToString()] = Convert.ToDateTime(field.Value);
                        item.UpdateOverwriteVersion();
                        break;

                    case FieldObject.Types.Number:
                        item[key.ToString()] = Convert.ToDecimal(field.Value);
                        item.UpdateOverwriteVersion();
                        break;

                    case FieldObject.Types.Boolean:
                        item[key.ToString()] = Convert.ToBoolean(field.Value);
                        item.UpdateOverwriteVersion();
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "AddItemToList", "ClsHelper"));
                log.WriteToLog();
            }
        }
 /// <summary>
 /// delete a sharepoint item
 /// </summary>
 /// <param name="item">the item to delete</param>
 public void DeleteItem(SPListItem item)
 {
     if (item == null)
     {
         return;
     }
     try
     {
         item.Delete();
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteItem", "ClsHelper"));
         log.WriteToLog();
     }
 }
 /// <summary>
 /// delete all the items in a shrepoint list (don't forget to declare the constructor)
 /// </summary>
 public void DeleteAllItemsInList()
 {
     try
     {
         SPListItemCollection itemCollection = GetItemCollection();
         if (itemCollection == null)
         {
             return;
         }
         DeleteItems(itemCollection);
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteAllItemsInList", "ClsHelper"));
         log.WriteToLog();
     }
 }
 public void DeleteGroup(string groupName)
 {
     try
     {
         using (SPWeb website = GetWebSite())
         {
             SPGroupCollection sharepointGroups = website.SiteGroups;
             sharepointGroups.Remove(groupName);
             website.Update();
         }
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "aGroupName", "ClsHelper"));
         log.WriteToLog();
     }
 }
Exemplo n.º 23
0
        public SPListItemCollection GetItemCollection(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                return(null);
            }

            SPListItemCollection items = null;

            try
            {
                SPList sharepointList = GetList();

                if (sharepointList == null)
                {
                    return(null);
                }

                /*bool enabledThrottling = sharepointList.EnableThrottling;
                 *
                 * if (enabledThrottling)
                 * {
                 *  sharepointList.EnableThrottling = false;
                 * }*/

                var sharepointQuery = new SPQuery
                {
                    Query = query,
                    // QueryThrottleMode = SPQueryThrottleOption.Override
                };

                items = sharepointList.GetItems(sharepointQuery);

                /*if (enabledThrottling)
                 * {
                 *  sharepointList.EnableThrottling = true;
                 * }*/
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetItemCollection", "ClsHelperParent"));
                log.WriteToLog();
            }

            return(items);
        }
        public bool IsViewExist(string view)
        {
            try
            {
                SPList list = GetList();

                if (list.Views.Cast <SPView>().Any(listView => listView.ToString() == view))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "IsViewExist", "ClsHelper"));
                log.WriteToLog();
            }

            return(false);
        }
        public void AddGroup(string groupName, string owner, string defaultUser, string description,
                             UserObject.RoleDefinition role)
        {
            if (string.IsNullOrEmpty(groupName))
            {
                return;
            }
            if (string.IsNullOrEmpty(owner))
            {
                return;
            }
            if (string.IsNullOrEmpty(defaultUser))
            {
                return;
            }
            try
            {
                using (SPWeb website = GetWebSite())
                {
                    SPUser sharepointOwner = GetUserObject(owner);
                    if (sharepointOwner == null)
                    {
                        return;
                    }
                    SPUser sharepointDefaultUser = GetUserObject(defaultUser);
                    if (sharepointDefaultUser == null)
                    {
                        return;
                    }
                    SPGroupCollection sharepointGroups = website.SiteGroups;
                    sharepointGroups.Add(groupName, sharepointOwner, sharepointDefaultUser, description);
                    AssignPermission(role, groupName, string.Empty);

                    website.Update();
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "AddGroup", "ClsHelper"));
                log.WriteToLog();
            }
        }
        public long GetFarmUsersCount()
        {
            long count = 0;

            try
            {
                UserProfileManager profileManager = GetProfileManager();
                if (profileManager != null)
                {
                    count = profileManager.Count;
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetUsersCount", "ClsHelper"));
                log.WriteToLog();
            }

            return(count);
        }
 public void DeleteFarmUser(string userAccount)
 {
     if (string.IsNullOrEmpty(userAccount))
     {
         return;
     }
     try
     {
         UserProfileManager profileManager = GetProfileManager();
         if (profileManager.UserExists(userAccount))
         {
             profileManager.RemoveUserProfile(userAccount);
         }
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "DeleteUser", "ClsHelper"));
         log.WriteToLog();
     }
 }
        public void UploadFileToLibrary(string destinationLibraryUrl, string filePathOnDisk)
        {
            if (string.IsNullOrEmpty(destinationLibraryUrl))
            {
                return;
            }

            if (string.IsNullOrEmpty(filePathOnDisk))
            {
                return;
            }

            FileStream stream  = null;
            SPWeb      website = null;

            try
            {
                stream = File.OpenRead(filePathOnDisk);
                var contents = new byte[stream.Length];
                stream.Read(contents, 0, (int)stream.Length);
                website = GetWebSite();
                website.Files.Add(destinationLibraryUrl, contents);
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "UploadFileToLibrary", "ClsHelper"));
                log.WriteToLog();
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (website != null)
                {
                    website.Dispose();
                }
            }
        }
 public void UpdateUser(UserObject user)
 {
     if (user == null)
     {
         return;
     }
     if (string.IsNullOrEmpty(user.UserName))
     {
         return;
     }
     try
     {
         SPUser sharepointUser = GetUserObject(user.UserName);
         if (sharepointUser == null)
         {
             return;
         }
         if (user.Email != string.Empty)
         {
             sharepointUser.Email = user.Email;
         }
         if (user.Name != string.Empty)
         {
             sharepointUser.Name = user.Name;
         }
         if (user.Notes != string.Empty)
         {
             sharepointUser.Notes = user.Notes;
         }
         sharepointUser.Update();
     }
     catch (Exception ex)
     {
         var log = new AppEventLog(AppException.ExceptionMessage(ex, "UpdateUser", "ClsHelper"));
         log.WriteToLog();
     }
 }
        public SPUser GetUserObject(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(null);
            }

            SPUser user = null;

            try
            {
                using (SPWeb website = GetWebSite())
                {
                    user = website.AllUsers[userName];
                }
            }
            catch (Exception ex)
            {
                var log = new AppEventLog(AppException.ExceptionMessage(ex, "GetUserObject", "ClsHelper"));
                log.WriteToLog();
            }

            return(user);
        }