SharePoint 2016与外部系统人员信息同步(一,Excel数据导入到AD)
2017-06-27 20:13
525 查看
SharePoint 2016 AD 数据同步(一,数据导入)
class Program { static void Main(string[] args) { string ldapPath = "LDAP://10.xxx.xxx.xxx/DC=xxxx,DC=com"; string userName = "Administrator"; string password = "Admin123"; //reference http://www.cnblogs.com/CSharpDevelopers/p/3634635.html //测试查找OU DirectoryEntry rootDirectoryEntry; if (userName != string.Empty) { rootDirectoryEntry = new DirectoryEntry(ldapPath, userName, password); } else { rootDirectoryEntry = new DirectoryEntry(ldapPath); } DataTable dt = GetTableFromExcel("D:\\SPCode_dct\\SPPM\\ADusers.xlsx"); //设定OU名 string currentValue = "FromCode"; //先检查是否存在。 try { DirectoryEntry getOuName = rootDirectoryEntry.Children.Find("OU = "+currentValue);//判断OU是否存在 if (getOuName != null) { return; } } catch( Exception ex) { } //foreach (DirectoryEntry getOuName1 in entry.Children) //{ // string ouName = getOuName1.Name; //} DirectoryEntry currentOuDirectoryEntry = rootDirectoryEntry.Children.Add("OU=" + currentValue, "organizationalUnit"); currentOuDirectoryEntry.Properties["name"].Add(currentValue); currentOuDirectoryEntry.CommitChanges(); //检查用户是否存在 foreach (DataRow dr in dt.Rows) { string sAMAccountName = dr["用户名"].ToString(); string displayName = dr["显示名"].ToString(); string newUserDefaultPassword = dr["pwd"].ToString(); DirectorySearcher userDirectorySearcher = new DirectorySearcher(currentOuDirectoryEntry, string.Format(@"(&(cn={0})(objectCategory=person)(objectClass=user))", displayName), new[] { "adspath" }, SearchScope.OneLevel); SearchResult searchResult = userDirectorySearcher.FindOne(); if (searchResult == null) { using (DirectoryEntry currentUserDirectoryEntry = currentOuDirectoryEntry.Children.Add("CN=" + displayName, "user")) { currentUserDirectoryEntry.Properties["sAMAccountName"].Value = sAMAccountName; currentUserDirectoryEntry.Properties["userPrincipalName"].Value = string.Format(@"{0}@{1}", sAMAccountName, "spdev"); currentUserDirectoryEntry.Properties["displayName"].Value = displayName; currentUserDirectoryEntry.CommitChanges(); //currentUserDirectoryEntry.Properties["userAccountControl"].Value = userAccountControl; currentUserDirectoryEntry.Properties["pwdLastSet"].Value = 0; currentUserDirectoryEntry.Invoke("SetPassword", new object[] { newUserDefaultPassword });//初始密码,要注意密码策略例如123.com 是可以的。 currentUserDirectoryEntry.CommitChanges(); } } } } private static DataTable GetTableFromExcel(string fileName) { DataTable dataTable = new DataTable(); string connectionString = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source ={0};Extended Properties='Excel 12.0 Xml;HDR=YES'", fileName); //当这里提示错误的时候请下载,AccessDatabaseEngine.exe using (OleDbConnection oleDbConnection = new OleDbConnection(connectionString)) { oleDbConnection.Open(); DataTable schemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" }); string sheetName = schemaTable.Rows[0].Field<string>("TABLE_NAME"); string commandText = string.Format("select * from [{0}]", sheetName); using (OleDbDataAdapter adapter = new OleDbDataAdapter(commandText, oleDbConnection)) { adapter.Fill(dataTable); } } return dataTable; } }
相关文章推荐
- asp.net系统导入Excel数据时提示:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序"
- 从SharePoint列表往Excel导入数据
- 如何将域中的AD数据导入SharePoint
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表 .
- VB.Net机房收费系统(三层)——将datagirdview 控件数据导入Excel
- [FIM]如何从A导入数据,同步到B,在A系统中删除数据,在B系统中保留数据并修改状态
- SharePoint 2010 EXCEL数据导入到List
- sharepoint 2010 User Profile Service 与AD用户数据同步
- [FIM]如何从A导入数据,同步到B,在A系统中删除数据,在B系统中删除数据
- 将AD中的数据同步到SharePoint 2013
- 导入Excle数据至数据库 “外部表不是预期的格式”错误信息
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表
- asp.net系统导入Excel数据时提示:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序"
- EXCEL数据导入TIPTOP ERP 系统
- MOSS2007用户配置文件从AD导入(选项卡无法搜索到人员),系统事件日志错误:Event ID 7888,Access Denied! Only site admin can access Data Source object from user profile DB
- 导入Excle数据至数据库 “外部表不是预期的格式”错误信息
- SharePoint 2007 系列(11) 导入Excel数据到SharePoint List
- asp.net系统导入Excel数据时提示:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序"
- sharepoint通过excel导入list时报错:导入数据时(方法'Post'作用于对象'IOWSPostData'时失败)
- sql批量导入外部数据excel到sqlserver