1、为所有独立权限的文件夹及其子文件和文件夹设置继承权限
2、为所有独立权限的文件夹及其子文件和文件夹添加用户
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
using System.Reflection;
namespace ResetPermissions_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btInheritRole_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void gb1_Enter(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void rb1_CheckedChanged(object sender, EventArgs e)
{
lb2.Visible = false;
lb3.Visible = false;
tbAccount.Visible = false;
tbPL.Visible = false;
lb4.Visible = false;
otherPermission.Visible = false;
}
private void rb2_CheckedChanged(object sender, EventArgs e)
{
lb2.Visible = true;
lb3.Visible = true;
tbAccount.Visible = true;
tbPL.Visible = true;
}
private void bt1_Click(object sender, EventArgs e)
{
InitionFile();
if (rb1.Checked)
{
resetInheritance();
}
if (rb2.Checked)
{
addUserRole();
}
if (!rb1.Checked & !rb2.Checked)
{
MessageBox .Show("Please Select "+"'"+rb1.Text+"'"+" or "+"'"+rb2.Text+"'"+"!");
CloseFile();
return;
}
CloseFile();
MessageBox.Show("Perform Complete!");
}
string account = null;
private void tbAccount_TextChanged(object sender, EventArgs e)
{
account = tbAccount.Text.ToString();
}
string weburl = null;
private void tbUrl_TextChanged(object sender, EventArgs e)
{
weburl = tbUrl.Text.ToString();
}
string otherPermission1 = null;
private void otherPermission_TextChanged(object sender, EventArgs e)
{
otherPermission1 = otherPermission.Text.ToString();
}
static StreamWriter writer;
static void InitionFile()
{
writer = new StreamWriter("ResetPermissionsLog.txt", true);
}
static void CloseFile()
{
writer.Flush();
writer.Close();
}
static void WriteLine(string msg)
{
writer.WriteLine(msg);
}
private void resetInheritance()
{
using (SPSite site = new SPSite(weburl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetListFromUrl(weburl);
string folderString = null;
if (weburl.Contains("&"))
{
folderString = Regex.Match(weburl, "RootFolder=.+?&").Value;
}
else
{
folderString = Regex.Match(weburl, "RootFolder=.+").Value;
}
Console.WriteLine("{0}", folderString);
if (folderString == null)
{
folderString = weburl;
}
else
{
folderString = folderString.Replace("RootFolder=", "");
if (folderString.Contains("&"))
{
folderString = folderString.Replace("&", "");
}
}
SPFolder folder = web.GetFolder(folderString);
SPQuery query = new SPQuery();
query.Folder = folder;
query.ViewAttributes = "Scope=\"RecursiveAll\"";
query.Query = "
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
if (item.HasUniqueRoleAssignments)
{
item.ResetRoleInheritance();
writer.WriteLine("{0}\t{1}\t{2}\t{3}", DateTime.Now, item.Name, web.Url ,item.Url);
ListViewItem listItem = new ListViewItem();
//listItem.SubItems.Add(DateTime.Now.ToString());
listItem.SubItems.Add(item.Name);
listItem.SubItems.Add(web.Url);
listItem.SubItems.Add(item.Url);
listItem.SubItems.Add("-");
listItem.SubItems.Add("-");
// listView1.Items.AddRange(new ListViewItem[] { listItem });
listView1.Items.Add(listItem);
}
}
}
}
}
private void addUserRole()
{
using (SPSite site = new SPSite(weburl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetListFromUrl(weburl);
string folderString = null;
if (weburl.Contains("&"))
{
folderString = Regex.Match(weburl, "RootFolder=.+?&").Value;
}
else
{
folderString = Regex.Match(weburl, "RootFolder=.+").Value;
}
Console.WriteLine("{0}", folderString);
if (folderString == null)
{
folderString = weburl;
}
else
{
folderString = folderString.Replace("RootFolder=", "");
if (folderString.Contains("&"))
{
folderString = folderString.Replace("&", "");
}
}
SPFolder folder = web.GetFolder(folderString);
SPQuery query = new SPQuery();
query.Folder = folder;
query.ViewAttributes = "Scope=\"RecursiveAll\"";
query.Query = "
SPListItemCollection items = list.GetItems(query);
int pl1 = tbPL.SelectedIndex;
SPRoleDefinition mPermissionLevel = null;
switch (pl1)
{
case 3:
mPermissionLevel = web.RoleDefinitions[otherPermission1];
break;
case 2:
mPermissionLevel = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
break;
case 1:
mPermissionLevel = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
break;
case 0:
mPermissionLevel = web.RoleDefinitions.GetByType(SPRoleType.Reader);
break;
}
try
{
SPUser user = null;
SPGroup group = null;
if (account.Contains("\\"))
{
user = web.EnsureUser(account);
}
else
{
group = web.SiteGroups[account];
}
{
foreach (SPListItem item in items)
{
if (item.HasUniqueRoleAssignments)
{
SPRoleAssignment ra = null;
if (group == null)
{
ra = new SPRoleAssignment(user);
}
else
{
ra = new SPRoleAssignment(group);
}
//SPRoleDefinition mPermissionLevel = web.RoleDefinitions.GetByType(SPRoleType.Reader);
ra.RoleDefinitionBindings.Add(mPermissionLevel);
item.RoleAssignments.Add(ra);
writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", DateTime.Now, item.Name, web.Url,item.Url, account, mPermissionLevel.Name);
ListViewItem listItem = new ListViewItem();
// listItem.SubItems.Add(DateTime.Now.ToString());
listItem.SubItems.Add(item.Name);
listItem.SubItems.Add(web.Url);
listItem.SubItems.Add(item.Url);
listItem.SubItems.Add(account);
listItem.SubItems.Add(mPermissionLevel.Name);
listView1.Items.AddRange(new ListViewItem[] { listItem });
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}
private void bt2_Click(object sender, EventArgs e)
{
//string saveFileName = "";
// SaveFileDialog saveFileDialog1 = new SaveFileDialog();
// saveFileDialog1.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
// if (saveFileDialog1.ShowDialog() == DialogResult.OK)
// {
// SaveFile(saveFileDialog.FileName);
// }
saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd");
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
TurnToExcel(listView1, "LOG");
}
}
public void TurnToExcel(ListView listView1, string sheet1)
{
string Sheetname = sheet1;
ListView listView = listView1;
if (listView.Items.Count < 1)
return;
try
{
ExcelApplication MyExcel = new ExcelApplication();
MyExcel.Visible = true;
if (MyExcel == null)
{
return;
}
Workbooks MyWorkBooks = (Workbooks)MyExcel.Workbooks;
Workbook MyWorkBook = (Workbook)MyWorkBooks.Add(Missing.Value);
Worksheet MyWorkSheet = (Worksheet)MyWorkBook.Worksheets[1];
Range MyRange = MyWorkSheet.get_Range("A1", "H1");
MyRange = MyRange.get_Resize(1, listView.Columns.Count);
object[] MyHeader = new object[listView.Columns.Count];
for (int i = 0; i < listView.Columns.Count; i++)
{
MyHeader.SetValue(listView.Columns[i].Text, i);
}
MyRange.Value2 = MyHeader;
MyWorkSheet.Name = Sheetname;
if (listView.Items.Count > 0)
{
MyRange = MyWorkSheet.get_Range("A2", Missing.Value);
object[,] MyData = new Object[listView.Items.Count, listView.Columns.Count];
for (int j = 0; j < listView1.Items.Count; j++)
{
ListViewItem lvi = listView1.Items[j];
for (int k = 0; k < listView.Columns.Count; k++)
{
MyData[j, k] = lvi.SubItems[k].Text;
}
}
MyRange = MyRange.get_Resize(listView.Items.Count, listView.Columns.Count);
MyRange.Value2 = MyData;
MyRange.EntireColumn.AutoFit();
}
try
{
object missing = System.Reflection.Missing.Value;
MyWorkBook.Saved = true;
MyWorkBook.SaveAs(saveFileDialog1.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, missing, missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
}
catch (Exception e1)
{
MessageBox.Show("Export Error,Maybe the file is opened by other application!\n" + e1.Message);
return;
}
/*
finally
{
MyExcel.Quit();
System.GC.Collect();
}
*/
// MyExcel = null;
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
return;
}
}
private void tbPL_SelectedIndexChanged(object sender, EventArgs e)
{
if (tbPL.SelectedIndex == 3)
{
lb4.Visible = true;
otherPermission.Visible = true;
}
else
{
lb4.Visible = false;
otherPermission.Visible = false;
}
}
}
}