Therefore , I developed winform solution, which helps me.
First of all, I use BackgroundWorker just to execute the operation on a separated thread.
Second, I aggregate fields from the form, which help me to bring a relevant data:
SiteURL - www.contoso.com
List - myList
ItemField - SPField("Display Name") For example item["ModifiedBy"]
ItemField Text - For example item["ModifiedBy"] = "Victor"
User - I use SPWeb.EnsureUser
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 Microsoft.SharePoint;
namespace GivenPermissions
{
public partial class Form1 : Form
{
BackgroundWorker bg2 = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void btnRun_Click(object sender, EventArgs e)
{
bg2.DoWork += new DoWorkEventHandler(bg2_DoWork);
bg2.ProgressChanged += new ProgressChangedEventHandler(bg2_ProgressChanged);
bg2.RunWorkerAsync();
}
private void bg2_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
}
void bg2_DoWork(object sender, DoWorkEventArgs e)
{
try
{
using (SPSite site = new SPSite(txtSiteUrl.Text))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[txtList.Text];
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
SPListItemCollection col = list.GetItems(query);
foreach (SPListItem item in col)
{
if (item[txtItemField.Text] != null && string.Compare(item[txtItemField.Text].ToString(), txtItemFielText.Text) == 0)
{
SPUser user = web.EnsureUser(txtUser.Text);
SPRoleType roleType = SPRoleType.Contributor;
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)user);
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
item.Update();
}
}
}
}
if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MethodInvoker(delegate
{
txtMessage.AppendText("DONE ! ! !");
}));
}
}
catch (Exception ex)
{
if (txtMessage.InvokeRequired)
{
txtMessage.Invoke(new MethodInvoker(delegate
{
txtMessage.AppendText(ex.Message + "\n" + ex.StackTrace);
}));
}
}
}
}
}
No comments:
Post a Comment