Example #1
0
        public static int GetLookupIdFromValue(this SPFieldLookup field, string fieldValue)
        {
            int ret = 0;

            try //assume fieldValue has both ID and Value
            {
                SPFieldLookupValue pairValue = new SPFieldLookupValue(fieldValue);

                if (pairValue != null && pairValue.LookupId != 0)
                {
                    return(pairValue.LookupId);
                }
            }
            catch
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb web = SPContext.Current.Site.OpenWeb(field.LookupWebId))
                    {
                        SPList list = web.Lists.GetList(new Guid(field.LookupList), false);
                        SPListItemCollection founds = list.FindItems(field.LookupField, fieldValue);
                        if (founds.Count > 0)
                        {
                            ret = founds[0].ID;
                        }
                    }
                });
            }
            return(ret);
        }