S
Smacky311
I would like to be able to submit a document to a document library, remove
all permissions for the document, then add permissions for 3 hard coded users
+ the user submitting the document. The current code works from a console
app, but not from infopath. I need help figuring out how to make the code
know the name of the form submitted and start removing the permissions after
it has been successfully uploaded (or an easier way to do this...).
try
{
SPSite WebApp = new SPSite("http://dev");
SPWeb web = WebApp.OpenWeb();
//SPWeb web =
SPControl.GetContextSite(Context).AllWebs["FinalSubmit"];
//SPWeb web2 = SPContext.Current.Web;
SPUser user = web.Users.GetByID(1073741823);
//SPUser user = web.CurrentUser; // the calling user
// Uses the SHAREPOINT\system creds with the SPUser's
identity reference of user
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Gets a new security context using SHAREPOINT\system
using (SPSite site = new SPSite(@"http://dev"))
//using (SPSite site = new
SPSite(this.Page.Request.Url.ToString()))
{
using (SPWeb thisWeb = site.OpenWeb())
//using (SPWeb thisWeb =
SPControl.GetContextSite(Context).AllWebs["FinalSubmit"])
{
thisWeb.AllowUnsafeUpdates = true;
SPList theList = thisWeb.Lists["FinalSubmit"];
SPListItem record = theList.Items[1];
//SPListItem record = theList.Items.Add();
record[0] = user;
//record.Update(); // uses SHAREPOINT\system
SPRoleAssignmentCollection perms =
theList.Items[1].RoleAssignments;
SPRoleDefinitionCollection roleDefinitions =
thisWeb.RoleDefinitions;
//Remove all permissions
int count = perms.Count;
for (int j = 0; j < count; j++)
{
perms.Remove(0);
}
//Add permissions
site.AllowUnsafeUpdates = true;
site.CatchAccessDeniedException = true;
string userEmail = @user.Email;
string userName = @user.Name;
string loginName = @user.LoginName;
SPRoleAssignment roleAssignment = new
SPRoleAssignment(@"st\ron", "[email protected]", "Ron Touple", "no notes");
SPRoleAssignment roleAssignment1 = new
SPRoleAssignment(@"st\bmackey", "[email protected]", "Brian Mackey",
"no notes");
SPRoleAssignment roleAssignment2 = new
SPRoleAssignment(@"st\brad", "[email protected]", "Brad Cook", "no
notes");
SPRoleAssignment roleAssignment3 = new
SPRoleAssignment(loginName, userEmail, userName, "no notes");
SPRoleDefinitionBindingCollection
roleDefBindings = roleAssignment.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings1 = roleAssignment1.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings2 = roleAssignment2.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings3 = roleAssignment3.RoleDefinitionBindings;
roleDefBindings.Add(roleDefinitions["Contribute"]);
roleDefBindings1.Add(roleDefinitions["Contribute"]);
roleDefBindings2.Add(roleDefinitions["Contribute"]);
roleDefBindings3.Add(roleDefinitions["Contribute"]);
perms.Add(roleAssignment);
perms.Add(roleAssignment1);
perms.Add(roleAssignment2);
perms.Add(roleAssignment3);
}
}
});
all permissions for the document, then add permissions for 3 hard coded users
+ the user submitting the document. The current code works from a console
app, but not from infopath. I need help figuring out how to make the code
know the name of the form submitted and start removing the permissions after
it has been successfully uploaded (or an easier way to do this...).
try
{
SPSite WebApp = new SPSite("http://dev");
SPWeb web = WebApp.OpenWeb();
//SPWeb web =
SPControl.GetContextSite(Context).AllWebs["FinalSubmit"];
//SPWeb web2 = SPContext.Current.Web;
SPUser user = web.Users.GetByID(1073741823);
//SPUser user = web.CurrentUser; // the calling user
// Uses the SHAREPOINT\system creds with the SPUser's
identity reference of user
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Gets a new security context using SHAREPOINT\system
using (SPSite site = new SPSite(@"http://dev"))
//using (SPSite site = new
SPSite(this.Page.Request.Url.ToString()))
{
using (SPWeb thisWeb = site.OpenWeb())
//using (SPWeb thisWeb =
SPControl.GetContextSite(Context).AllWebs["FinalSubmit"])
{
thisWeb.AllowUnsafeUpdates = true;
SPList theList = thisWeb.Lists["FinalSubmit"];
SPListItem record = theList.Items[1];
//SPListItem record = theList.Items.Add();
record[0] = user;
//record.Update(); // uses SHAREPOINT\system
SPRoleAssignmentCollection perms =
theList.Items[1].RoleAssignments;
SPRoleDefinitionCollection roleDefinitions =
thisWeb.RoleDefinitions;
//Remove all permissions
int count = perms.Count;
for (int j = 0; j < count; j++)
{
perms.Remove(0);
}
//Add permissions
site.AllowUnsafeUpdates = true;
site.CatchAccessDeniedException = true;
string userEmail = @user.Email;
string userName = @user.Name;
string loginName = @user.LoginName;
SPRoleAssignment roleAssignment = new
SPRoleAssignment(@"st\ron", "[email protected]", "Ron Touple", "no notes");
SPRoleAssignment roleAssignment1 = new
SPRoleAssignment(@"st\bmackey", "[email protected]", "Brian Mackey",
"no notes");
SPRoleAssignment roleAssignment2 = new
SPRoleAssignment(@"st\brad", "[email protected]", "Brad Cook", "no
notes");
SPRoleAssignment roleAssignment3 = new
SPRoleAssignment(loginName, userEmail, userName, "no notes");
SPRoleDefinitionBindingCollection
roleDefBindings = roleAssignment.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings1 = roleAssignment1.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings2 = roleAssignment2.RoleDefinitionBindings;
SPRoleDefinitionBindingCollection
roleDefBindings3 = roleAssignment3.RoleDefinitionBindings;
roleDefBindings.Add(roleDefinitions["Contribute"]);
roleDefBindings1.Add(roleDefinitions["Contribute"]);
roleDefBindings2.Add(roleDefinitions["Contribute"]);
roleDefBindings3.Add(roleDefinitions["Contribute"]);
perms.Add(roleAssignment);
perms.Add(roleAssignment1);
perms.Add(roleAssignment2);
perms.Add(roleAssignment3);
}
}
});