/// <summary> /// Parses xml nodes with user info into CS4User objects and returns them ina collection /// </summary> /// <param name="usersElements">user xml nodes</param> /// <returns>CS4UserCollection with the data from the xml</returns> private CS4UserCollection GetUsersFromXml(XmlNodeList usersElements) { CS4UserCollection users = new CS4UserCollection(); for (int curElementIndex = 0; curElementIndex < usersElements.Count; curElementIndex++) { string userLogin = usersElements[curElementIndex].Attributes["LoginName"].InnerText; string userDomain = usersElements[curElementIndex].Attributes["LoginNameDomain"].InnerText; string userName = usersElements[curElementIndex].Attributes["UserName"].InnerText; string userEmail = usersElements[curElementIndex].Attributes["Email"].InnerText; string userRole = usersElements[curElementIndex].Attributes["Role"].InnerText; bool userTransfer = (usersElements[curElementIndex].Attributes["Transfer"].InnerText == "1" ? true : false); int userId = 0; Int32.TryParse(usersElements[curElementIndex].Attributes["Id"].InnerText, out userId); CS4User user = new CS4User( userId, userDomain, userLogin, userName, userEmail, userTransfer, IsUserInTeacherRole(userRole)); users.Add(user); } return(users); }
/// <summary> /// Asyncronously calls VerifyAgainstActiveDirectory methos of ClassServerUsers class /// </summary> private void llCheckUsersInAD_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (!AllAppSettingsSet()) { lblCheckUsersInADStatus.Text = TextResources.AppSettingsNotSet; return; } m_users = new CS4UserCollection(); System.ComponentModel.BackgroundWorker usersCheck = new BackgroundWorker(); usersCheck.WorkerReportsProgress = true; usersCheck.DoWork += new DoWorkEventHandler(usersCheck_DoWork); usersCheck.RunWorkerCompleted += new RunWorkerCompletedEventHandler(usersCheck_RunWorkerCompleted); usersCheck.ProgressChanged += new ProgressChangedEventHandler(usersCheck_ProgressChanged); usersCheck.RunWorkerAsync(SiteBuilder.Default.ActiveDirectoryPath); }
/// <summary> /// Assigns users to a SharePoint v3 site according to permissions specified in the User object for every user /// </summary> /// <param name="SiteURL">SharePoint v3 site URL</param> /// <param name="Users">Users to be added to the site</param> /// <param name="Log">The method will return log of the operations performed.</param> public void AssignUsersToSite(string siteURL, CS4UserCollection users, ref string log) { for (int userIndex = 0; userIndex < users.Count; userIndex++) { try { AddSiteUser(users.Item(userIndex)); log += String.Format(TextResources.UserAddedToSite, users.Item(userIndex).UserLoginWithDomain, siteURL) + Environment.NewLine; } catch (System.Exception ex) { log += String.Format(TextResources.CantAddUserToSite, users.Item(userIndex).UserLoginWithDomain, siteURL) + ex.Message + Environment.NewLine; } } }
/// <summary> /// Adds users to SharePoint group. If user does not belong to the site, it will be added. /// </summary> /// <param name="SiteURL">A SharePoint site where the group resides</param> /// <param name="GroupName">SharePoint group name</param> /// <param name="Users">List of users to be added</param> /// <param name="Log">Method returns a log of operations performed</param> public void AssignUsersToGroup(string siteURL, string groupName, CS4UserCollection users, ref string log) { SPSite site = null; SPWeb siteWeb = null; SPGroup group = null; try { site = new SPSite(siteURL); siteWeb = site.OpenWeb(); group = siteWeb.SiteGroups[groupName]; } catch (System.Exception ex) { log += String.Format(TextResources.CantAddUsersToGroup, groupName) + ex.Message + Environment.NewLine; return; } //ensure user has access to the site for (int userIndex = 0; userIndex < users.Count; userIndex++) { SPUser user = null; bool createUser = false; try { user = siteWeb.SiteUsers[users.Item(userIndex).UserLoginWithDomain]; } catch { //user does not exist createUser = true; } try { if (createUser) { AddSiteUser(users.Item(userIndex)); user = siteWeb.SiteUsers[users.Item(userIndex).UserLoginWithDomain]; } group.AddUser(user); log += String.Format(TextResources.UserAddedToGroup, users.Item(userIndex).UserLoginWithDomain, groupName) + Environment.NewLine; } catch (System.Exception ex) { log += String.Format(TextResources.CantAddUserToGroup, users.Item(userIndex).UserLoginWithDomain, groupName) + ex.Message + Environment.NewLine; } } }
/// <summary> /// Does the actual work of creating an SLK site with groups and users. /// </summary> /// <param name="classData">Class to be created</param> /// <param name="processingLog">Method returns log of actions performed</param> private void ProcessClass(CS4Class classData, ref string processingLog) { string log = System.String.Empty; SharePointV3 SLKweb = new SharePointV3(); CS4UserCollection classUsers = new CS4UserCollection(); CS4UserCollection groupUsers = new CS4UserCollection(); bool classAdded = false; string classURL = String.Empty; string classCreateResult = String.Empty; CS4User groupsOwner = null; log += System.Environment.NewLine + TextResources.ProcessingClass + classData.ClassName + System.Environment.NewLine; if (classData.Transfer) { classAdded = SLKweb.CreateSite( SiteBuilder.Default.SLKSchoolWeb, classData.ClassWeb, classData.ClassName, String.Empty, classData.ClassLCID, classData.Overwrite, ref classURL, ref classCreateResult); log += classCreateResult; if (classAdded) { //adding site users for (int classUserIndex = 0; classUserIndex < classData.Users.Count; classUserIndex++) { CS4User user = classData.Users.Item(classUserIndex); if (user.Transfer) { user.UserRoles = this.DefineUserRoles(user.IsTeacher, classURL); classUsers.Add(user); //if the user is teacher, set it as group owner //we only take first teacher to be groups owner as we only need one if ((groupsOwner == null) && (user.IsTeacher)) { groupsOwner = user; } } else { log += string.Format(TextResources.UserNotForTransfer, user.UserLoginWithDomain) + System.Environment.NewLine; } } string addUsersLog = String.Empty; SLKweb.AssignUsersToSite(classURL, classUsers, ref addUsersLog); log += addUsersLog; //adding groups for (int groupIndex = 0; groupIndex < classData.Groups.Count; groupIndex++) { log += string.Format(TextResources.ProcessingGroup, classData.Groups.Item(groupIndex).WebName) + System.Environment.NewLine; if (classData.Groups.Item(groupIndex).Transfer) { //processing group users groupUsers = new CS4UserCollection(); for (int groupUserIndex = 0; groupUserIndex < classData.Groups.Item(groupIndex).GroupUsers.Count; groupUserIndex++) { CS4User groupUser = classData.Groups.Item(groupIndex).GroupUsers.Item(groupUserIndex); if (groupUser.Transfer) { groupUser.UserRoles = this.DefineUserRoles(groupUser.IsTeacher, classURL); groupUsers.Add(groupUser); } else { log += string.Format(TextResources.GroupUserNotForTransfer, groupUser.UserLoginWithDomain) + System.Environment.NewLine; } } //adding group //only if we have a group owner and at least one group user if ((groupUsers.Count > 0) && (groupsOwner != null)) { //taking first user as default user bool groupAdded = false; string groupCreateResult = string.Empty; groupAdded = SLKweb.CreateUsersGroup( classURL, classData.Groups.Item(groupIndex).WebName, groupsOwner, classData.Groups.Item(groupIndex).GroupUsers.Item(0), classData.Groups.Item(groupIndex).Overwrite, ref groupCreateResult); //adding group users log += groupCreateResult; if (groupAdded) { string addGroupUsersLog = String.Empty; SLKweb.AssignUsersToGroup(classURL, classData.Groups.Item(groupIndex).WebName, groupUsers, ref addGroupUsersLog); log += addGroupUsersLog; } } else { //not transferring this group log += TextResources.GroupOwnerOrDefaultUserProblem + System.Environment.NewLine; } } else { log += TextResources.GroupNotForTransfer + System.Environment.NewLine; } } } } else { log += TextResources.ClassNotForTransfer + System.Environment.NewLine; } log += String.Format(TextResources.FinishedProcessingClass, classData.ClassName) + System.Environment.NewLine; processingLog = log; }