1

Ok, so I have a form that has multiple views, some read only and some editable. Each form has buttons to open the other forms in either edit mode or read only mode based on a field being set to either 1 or 0 and the current status of the form (so if the status is 'Awaiting Purchasing' and the check field is 1, then clicking the purchasing button opens the purchasing view in edit mode.

I have a button (Edit) to press that sets the check field to 1 on click and submitting the form sets the field back to 0.

Now here comes the tricky part! I have a list that contains Department (text) and Editors (multi-select people field). I need, within the Edit button, to check the status of the form, then check if the current user exists in the Editors field from the other list based on that status. If they exists then the Edit button will set the check field to 1, otherwise it won't.

Can anyone tell me a) if that is possible and b) if so, how to accomplish it?

  • When you say "I have a form that..", do you mean in Infopath ? – MdMazzotti Mar 6 '14 at 19:21
  • Check the tags, they are clearly stating infopath, as well as infopath rules. – Christoffer Mar 7 '14 at 7:29
0

this is a simplified version i made for you but havent tested it! so might need some tweaking ;)

You dont need to change the second method! for the first method, replace yourSiteGoesHere with your site name or leave blank. change ListName to the name of your list.

you would call the method like so:

    //returns bool if the current user exists in department "marketing" (true=1 , false=0)
    bool isingroup = getAccessFromGroup("Marketing");

that would call the methods below!

    /// <summary>
    /// returns true if user is in field within list
    /// </summary>
    /// <returns></returns>
    public bool getAccessFromGroup(string department)
    { 
        //open site from current site context
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            //open specific site, leave blank if toplevel
            using (SPWeb web = site.OpenWeb("yourSiteGoesHere"))
            {
                //get splist collection but only return one row based on query
                SPListItemCollection currentUser = getListDetails("ListName", web, SPContext.Current.Web.CurrentUser.LoginName);

                //if null then user doesnt exist on any items
                if (currentUser != null)
                {

                    //check if the user is in the department, loop if user exist in more than one place
                    foreach (SPListItem item in currentUser)
                    {
                        //check to make sure the field department matches the one you want else loop through to end and return false (user doesnt exist)
                        if (item["Department"] == department)
                        {
                            //return true as use exists in department
                            return true;
                        }
                    }   
                }                 
            }
        }
        return false;
    }

this is called from the method above! no need to change anything unless i have the field name 'Editors' wrong?

    private SPListItemCollection getListDetails(string listName, SPWeb web, string passVal)
    {
        SPList List = web.Lists[listName];
        SPQuery listABQuery = new SPQuery();
        listABQuery.Query = "<Where><Eq><FieldRef Name='Editors'/>" +
            "<Value Type='Text'>" + passVal + "</Value></Eq></Where>";
        SPListItemCollection item = List.GetItems(listABQuery);
        return item;
    }
  • Thanks ali, after reading through that, it looks like it does what I want, but here comes a daft question. Given that I'm fairly new to Sharepoint/Infopath, where exactly do I put the code methods and how do I call them from the button? I see a custom code button on the ribbon for the button but it's greyed out (I'm assuming this is because I have a formatting rule on the button that hides it when the check field is set to 1?). – CodenameSnugs Mar 6 '14 at 16:38
  • I, too, would be interested to know how this is supposed to apply to Infopath – MdMazzotti Mar 6 '14 at 19:25
  • You can only use custom code in InfoPath when working against a SharePoint Form Library. See this blogpost that explaining the differences between SharePoint List Forms and SharePoint Library Forms. blogs.msdn.com/b/infopath/archive/2010/04/22/… – Christoffer Mar 7 '14 at 7:32
  • also like to add this tutorial, step by step guide! johanolivier.blogspot.co.uk/2010/08/… – Ali Jafer Mar 7 '14 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.