BruceLee
DocumentDiscussion

导航

 

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Navigation;

namespace EDS.EntComm.DataProviders
{
    /// <summary>
    ///
    /// </summary>
    public class ChannelDAL
    {
        #region Create EDS' channel.
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-8-25
        /// Description:Create a web site,as is EDS' channel.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spRootWeb"></param>
        /// <param name="strWebUrl"></param>
        /// <param name="strTitle"></param>
        /// <param name="strDescription"></param>
        /// <param name="nLCID"></param>
        /// <param name="strWebTemplate"></param>
        /// <param name="useUniquePermissions"></param>
        /// <param name="bConvertIfThere"></param>
        /// <returns></returns>
        public bool CreateWebSite(SPWeb spRootWeb, string strWebUrl, string strTitle, string strDescription, uint nLCID, string strWebTemplate, bool useUniquePermissions, bool bConvertIfThere)
        {
            SPFile spFileRoot = null;
            bool boolReturn = false;
            SPWebCollection spWebCollection = null;
            SPWeb newWeb = null;
            PublishingWeb rootPublishingWeb = null;
            PublishingWeb newPublishingWeb = null;

            try
            {
                spRootWeb.AllowUnsafeUpdates = true;
                spWebCollection = spRootWeb.Webs;
                if (PublishingWeb.IsPublishingWeb(spRootWeb))
                {
                    rootPublishingWeb = PublishingWeb.GetPublishingWeb(spRootWeb);
                    spFileRoot = rootPublishingWeb.DefaultPage;
                    rootPublishingWeb.DefaultPage = spRootWeb.GetFile("Pages/default.aspx");
                    rootPublishingWeb.Update();
                }
                newWeb = spWebCollection.Add(strWebUrl, strTitle, strDescription, nLCID, strWebTemplate, useUniquePermissions, bConvertIfThere);
                newWeb.AllowUnsafeUpdates = true;
                newPublishingWeb = PublishingWeb.GetPublishingWeb(newWeb);
                newPublishingWeb.DefaultPage = newWeb.GetFile("ChannelDefault.aspx"); ;
                newPublishingWeb.Update();
                rootPublishingWeb.DefaultPage = spFileRoot;
                rootPublishingWeb.Update();
                boolReturn = true;
                spRootWeb.AllowUnsafeUpdates = false;
                newWeb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spRootWeb.Dispose();
                newWeb.Dispose();
            }
            return boolReturn;
        }
        #endregion
        #region Get spweb
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-9-3
        /// Description:get spweb.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spRootWeb"></param>
        /// <param name="strWebUrl"></param>
        /// <returns></returns>
        public SPWeb GetSPWeb(SPWeb spRootWeb, string strWebUrl)
        {
            SPWeb returnSPWeb = null;
            try
            {
                returnSPWeb = spRootWeb.Webs[strWebUrl];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return returnSPWeb;
        }
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-9-3
        /// Description:get spweb.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="strSiteURL"></param>
        /// <param name="strWebUrl"></param>
        /// <returns></returns>
        public SPWeb GetSPWeb(string strSiteURL, string strWebUrl)
        {
            SPSite spSite = null;
            SPWeb returnSPWeb = null;
            try
            {
                spSite = new SPSite(strSiteURL);
                returnSPWeb = spSite.OpenWeb(strWebUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return returnSPWeb;
        }
        #endregion
        #region Create a user group in web site.
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-8-25
        /// Description:Create a user group in web site.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb"></param>
        /// <param name="strUserGropu"></param>
        /// <returns></returns>
        public bool CreateUserGroup(SPWeb spWeb, string strUserGroup)
        {
            bool boolReturn = false;
            try
            {

                SPMember spUserMember = null;
                SPSite spSite = spWeb.Site;
                spSite.AllowUnsafeUpdates = true;
                spWeb.AllowUnsafeUpdates = true;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPUser spUser = spWeb.CurrentUser;
                    spUserMember = spWeb.SiteAdministrators[0];
                    spWeb.SiteGroups.Add(strUserGroup, spUserMember, null, strUserGroup);
                });

                spWeb.AllowUnsafeUpdates = false;
                spSite.AllowUnsafeUpdates = false;
                boolReturn = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return boolReturn;
        }
        #endregion
        #region Setup user group
        /// <summary>
        ///
        /// </summary>
        /// <param name="spWeb"></param>
        /// <param name="strUserGroup"></param>
        /// <returns></returns>
        public bool SetupUserGroup(SPWeb spWeb, string strUserGroup)
        {
            bool boolReturn = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPMember spUserGroupMember1 = null;
                    SPMember spUserGroupMember2 = null;
                    SPSite spSite = spWeb.Site;
                    spSite.AllowUnsafeUpdates = true;
                    spWeb.AllowUnsafeUpdates = true;
                    spWeb.RoleDefinitions.BreakInheritance(false, false);
                    SPRoleAssignmentCollection spRAC = spWeb.RoleAssignments;
                    spUserGroupMember1 = spWeb.SiteGroups[SPContext.Current.Web.Name + "专家组"];
                    spUserGroupMember2 = spWeb.SiteGroups[SPContext.Current.Web.Name + "用户组"];

                    SPRoleAssignment spRoleAssignment1 = new SPRoleAssignment((SPGroup)spUserGroupMember1);
                    SPRoleDefinition spRoleDefinition1 = spWeb.RoleDefinitions["专家组权限"];
                    spRoleAssignment1.RoleDefinitionBindings.Add(spRoleDefinition1);
                    spRAC.Add(spRoleAssignment1);
                    SPRoleAssignment spRoleAssignment2 = new SPRoleAssignment((SPGroup)spUserGroupMember2);
                    SPRoleDefinition spRoleDefinition2 = spWeb.RoleDefinitions["用户组权限"];
                    spRoleAssignment2.RoleDefinitionBindings.Add(spRoleDefinition2);
                    spRAC.Add(spRoleAssignment2);
                    spWeb.AllowUnsafeUpdates = false;
                    spSite.AllowUnsafeUpdates = false;
                });
                boolReturn = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return boolReturn;
        }
        public bool SetSite(SPWeb spWeb)
        {
            bool boolReturn = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite spSite = spWeb.Site;
                    spSite.AllowUnsafeUpdates = true;
                    spWeb.AllowUnsafeUpdates = true;
                    spWeb.RoleDefinitions.BreakInheritance(false, false);
                    SPRoleDefinition spRolleDefinition1 = new SPRoleDefinition();
                    spRolleDefinition1.Name = "专家组权限";
                    spRolleDefinition1.Description = "专家组权限";
                    spRolleDefinition1.BasePermissions = SPBasePermissions.FullMask ^ SPBasePermissions.ManagePermissions;
                    spWeb.RoleDefinitions.Add(spRolleDefinition1);

                    SPRoleDefinition spRolleDefinition2 = new SPRoleDefinition();
                    spRolleDefinition2.Name = "用户组权限";
                    spRolleDefinition2.Description = "用户组权限";
                    spRolleDefinition2.BasePermissions = SPBasePermissions.AddListItems;
                    spWeb.RoleDefinitions.Add(spRolleDefinition2);
                    spSite.AllowUnsafeUpdates = false;
                    spWeb.AllowUnsafeUpdates = false;
                    boolReturn = true;
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return boolReturn;
        }
        #endregion
        #region Add a user in user group
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-8-25
        /// Description:Add a user in user group.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb"></param>
        /// <param name="strUserName">@"SATYAMBRUCELEE\user2"</param>
        /// <returns></returns>
        public bool AddUserInGroup(SPWeb spWeb, string strUserGroup, string strUserName)
        {
            bool boolReturn = false;
            SPUser oUser = null;
            try
            {
                spWeb.AllowUnsafeUpdates = true;
                oUser = spWeb.Users[strUserName];
                spWeb.SiteGroups[strUserGroup].AddUser(oUser);
                spWeb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oUser = null;
                spWeb.Dispose();
            }
            return boolReturn;
        }
        #endregion
        #region Add a folder in list
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-8-25
        /// Description:Add a folder in list by time.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb"></param>
        /// <param name="strSPList"></param>
        /// <param name="dataTime"></param>
        /// <returns>0 folder is exists,1 succeed,3 is exception</returns>
        public int CreateFolderByMonth(SPWeb spWeb, string strSPList, string strFolderName)
        {
            int intReturn = 3;
            SPList spList = null;
            SPListItem spListItem = null;
            SPListItemCollection spFolderCollection = null;
            SPFolder rootFolder = null;
            SPFolder subFolder = null;
            try
            {
                spWeb.AllowUnsafeUpdates = true;
                spList = spWeb.Lists[strSPList];
                spFolderCollection = spList.Folders;
                rootFolder = spWeb.GetFolder(spList.RootFolder.ServerRelativeUrl);
                subFolder = spWeb.GetFolder(spList.RootFolder.ServerRelativeUrl + "/" + strFolderName);
                if (!subFolder.Exists)
                {
                    spListItem = spList.Folders.Add(rootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
                    spListItem["Title"] = strFolderName;
                    spListItem["CommListTopicType"] = 0;
                    spListItem.Update();
                    spWeb.AllowUnsafeUpdates = false;
                    intReturn = 1;
                }
                else
                {
                    intReturn = 0;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return intReturn;
        }
        #endregion
        #region Add a note
        /// <summary>
        /// Author:(Bruce Lee)
        /// Created Time:2008-9-2
        /// Description:Add a note.
        /// Mender:
        /// Modify Time:
        /// Modify Description:
        /// </summary>
        /// <param name="spWeb"></param>
        /// <param name="strSPList"></param>
        /// <param name="strFolderName"></param>
        /// <param name="commListEntity"></param>
        /// <returns></returns>
        public bool AddNote(SPWeb spWeb, string strSPList, string strFolderName, int intCommListTopicType, string strContentType, CommListEntity commListEntity)
        {
            bool boolReturn = false;
            SPListItem spListItem = null;
            SPFolder rootFolder = null;
            SPFolder subFolder = null;
            SPList spList = null;
            try
            {
                spWeb.AllowUnsafeUpdates = true;
                spList = spWeb.Lists[strSPList];
                rootFolder = spWeb.GetFolder(spList.RootFolder.ServerRelativeUrl);
                subFolder = spWeb.GetFolder(spList.RootFolder.ServerRelativeUrl + "/" + strFolderName);
                SPContentType spContentType = spWeb.AvailableContentTypes[strContentType];
                if (rootFolder.Exists && subFolder.Exists)
                {
                    if (commListEntity.ID == 0)
                    {
                        if (intCommListTopicType == 0)
                        {
                            spListItem = spList.Items.Add(spList.RootFolder.ServerRelativeUrl + "/" + strFolderName, SPFileSystemObjectType.Folder, null);
                        }
                        else
                        {
                            spListItem = spList.Items.Add(spList.RootFolder.ServerRelativeUrl + "/" + strFolderName, SPFileSystemObjectType.File, null);
                        }
                    }
                    else
                    {
                        spListItem = spList.GetItemById(commListEntity.ID);
                    }
                    spListItem["ContentTypeId"] = spContentType.Id;
                    spListItem["Title"] = commListEntity.CommListTopicTitle;
                    //spListItem["CommListAnswerExpert"] = commListEntity.CommListAnswerExpert;
                    //spListItem["CommListAvailabilityBeginTime"] = commListEntity.CommListAvailabilityBeginTime;
                    //spListItem["CommListAvailabilityEndTime"] = commListEntity.CommListAvailabilityEndTime;
                    //spListItem["CommListChannelName"] = commListEntity.CommListChannelName;
                    //spListItem["CommListIfExpertAnswer"] = commListEntity.CommListIfExpertAnswer;
                    //spListItem["CommListIfHidden"] = commListEntity.CommListIfHidden;
                    //spListItem["CommListIfPink"] = commListEntity.CommListIfPink;
                    //spListItem["CommListIfTop"] = commListEntity.CommListIfTop;
                    //spListItem["CommListKeyword"] = commListEntity.CommListKeyword;
                    //spListItem["CommListLastReTime"] = commListEntity.CommListLastReTime;
                    //spListItem["CommListNumber"] = commListEntity.CommListNumber;
                    //spListItem["CommListPicURL"] = commListEntity.CommListPicURL;
                    //spListItem["CommListPubTime"] = commListEntity.CommListPubTime;
                    //spListItem["CommListPubUserDepartment"] = commListEntity.CommListPubUserDepartment;
                    //spListItem["CommListPubUserID"] = commListEntity.CommListPubUserID;
                    //spListItem["CommListPubUserName"] = commListEntity.CommListPubUserName;
                    //spListItem["CommListReNumber"] = commListEntity.CommListReNumber;
                    //spListItem["CommListReUser"] = commListEntity.CommListReUser;
                    //spListItem["CommListReUserDepartment"] = commListEntity.CommListReUserDepartment;
                    //spListItem["CommListReUserID"] = commListEntity.CommListReUserID;
                    //spListItem["CommListTipicURL"] = commListEntity.CommListTipicURL;
                    spListItem["CommListTopicContent"] = commListEntity.CommListTopicContent;
                    spListItem["CommListTopicTitle"] = commListEntity.CommListTopicTitle;
                    //spListItem["CommListTopicType"] = commListEntity.CommListTopicType;
                    //spListItem["CommListType"] = commListEntity.CommListType;
                    spListItem.Update();
                    boolReturn = true;
                }
                spWeb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return boolReturn;
        }
        #endregion
        public bool AssignPermission(SPWeb spWeb, string strListName, int intItemID, List<SPUser> spListUser)
        {
            bool returnBool = false;
            try
            {
                SPList spList = spWeb.Lists[strListName];
                SPListItem spListItem = spList.Items.GetItemById(intItemID);

                SPRoleDefinition spRoleDefinition = new SPRoleDefinition ();

                foreach (SPRoleDefinition roleTemp in spWeb.RoleDefinitions)
                {
                    if (roleTemp.Name == "设计")
                    {
                        spRoleDefinition = roleTemp;
                        break;
                    }
                }

                spListItem.ResetRoleInheritance();
                if (!spListItem.HasUniqueRoleAssignments)
                {
                    spListItem.BreakRoleInheritance(true);
                }

                for (int j = 0; j < spListItem.RoleAssignments.Count; )
                {
                    spListItem.RoleAssignments.Remove(j);
                }

                foreach (SPUser spUser in spListUser)
                {
                    SPRoleAssignment roleAssignment = new SPRoleAssignment (spUser);
                    roleAssignment.RoleDefinitionBindings.Add(spRoleDefinition);
                    spListItem.RoleAssignments.Add(roleAssignment);
                }
                returnBool = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                spWeb.Dispose();
            }
            return returnBool;
        }
    }
}

posted on 2008-09-04 14:48  Bruce Lee  阅读(1400)  评论(0编辑  收藏  举报