Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to create an action in a SharePoint Infopath form which checks item permissions for the current user?

SharePoint workflows set permission on individual list items for various users, and I want to include a button for users who have edit permission on the current item to open another form view which allows them to make a specific update to the item.

share|improve this question

migrated from serverfault.com Aug 21 at 2:59

This question came from our site for professional system and network administrators.

1 Answer 1

I've found a solution (via a page on Delphi-ts.com which I had to get from the wayback machine), but it's not ideal (from my novice standpoint), as it requires addition of C# code to the form which 1) means I've had to recreate the form in a forms library, not an item list and 2) the form has to be uploaded via sharepoint central admin, not published direct from InfoPath, as it is set to run in full trust mode.

That said, the following addition to the Loading Event code does give the desired result:

 XPathNavigator myNav = this.MainDataSource.CreateNavigator();
        try
        {                
            if (e.InputParameters.ContainsKey("XmlLocation") && !SPContext.Current.Web.CurrentUser.IsSiteAdmin)  //Regardless of resident permissions, give SiteAdmins access
            {
                SPUser curUser = SPContext.Current.Web.CurrentUser;
                string path = e.InputParameters["XmlLocation"].ToString(); //get the location of the XML file and parse
                string lib = path.Substring(0, path.LastIndexOf("/"));
                string formName = path.Substring(path.LastIndexOf("/") + 1);
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    SPWeb web = site.OpenWeb(SPContext.Current.Web.ID); //grab the current web
                    SPListItem li = web.GetListItem(path.Replace(web.Url, "")); //grab the current list item using the

                    if (li.DoesUserHavePermissions(SPBasePermissions.EditListItems)) //check for permissions
                        myNav.SelectSingleNode("//my:UserMode", this.NamespaceManager).SetValue("edit"); //if it exists, set field to false
                    else
                        myNav.SelectSingleNode("//my:UserMode", this.NamespaceManager).SetValue("read"); //if it doesn't, set it to true
                }

            }
            else
                myNav.SelectSingleNode("//my:UserMode", this.NamespaceManager).SetValue("new or admin"); //if the XmlLocation doesn't exist, it's a new form so it's not Read Only
        }
        catch (Exception ex)
        {
            //WriteErrorToLog("FormEvents_Loading", "Problem setting user permissions", ex);
            myNav.SelectSingleNode("//my:UserMode", this.NamespaceManager).SetValue("err"); //on error stop the user from writing
        }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.