Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / September 2007

Tip: Looking for answers? Try searching our database.

Programming Item level permissions for a document library

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Smacky311 - 15 Sep 2007 17:40 GMT
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", "ron@cookconcrete.com", "Ron Touple", "no notes");
                               SPRoleAssignment roleAssignment1 = new
SPRoleAssignment(@"st\bmackey", "bmackey@cookconcrete.com", "Brian Mackey",
"no notes");
                               SPRoleAssignment roleAssignment2 = new
SPRoleAssignment(@"st\brad", "brad@cookconcrete.com", "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);
                               
                           }
                       }
                   });
S.Y.M. Wong-A-Ton - 16 Sep 2007 02:10 GMT
This is more of a SharePoint question than it is an InfoPath issue; you could
have posted your question in one of the SharePoint development
newsgroups/forums. You need SharePoint to deal with the file permissions and
not InfoPath. You can use an ItemAdded event handler on a document/form
library in SharePoint to do what you require. The event handler will know
which form was submitted, and you can retrieve the properties of the item
that was added through the event handler.
---
S.Y.M. Wong-A-Ton

> 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
[quoted text clipped - 88 lines]
>                         }
>                     });
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.