Search code examples
c#functionreturn-valuesharepoint-apielevated-privileges

What's wrong with this function. unable to return string value


public static string GetContentFromSPList(string cValueToFind)
{   
    string cValueFound = "";
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite("http://mysite"))
            {
                site.AllowUnsafeUpdates = true;
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;

                    SPList oListAbout = web.Lists["About"];
                    SPQuery oQuery = new SPQuery();

                    oQuery.Query = "<OrderBy><FieldRef Name='myField' /></OrderBy><Where><Eq><FieldRef Name='myField' /><Value Type='Choice'>" + cValueToFind + "</Value></Eq></Where>";

                    SPListItemCollection collListItems = oListAbout.GetItems(oQuery);

                    foreach (SPListItem oListItem in collListItems)
                    {
                        cValueFound = (oListItem["FieldContents"] != null ? oListItem["FieldContents"].ToString() : "");
                    }
                }
            }
            return cValueFound;
        });
        //return cValueFound;
    }
    catch (Exception ex)
    {
    }
    finally
    {
        //return cValueFound;
    }
}

Above is the piece of code.

Problem is not allowing to return the string. It keeps on giving compilation errors. I am sure I am doing something wrong!!.

Thanks.


Solution

  • I suppose it's something like:

    "not all codes return value".

    If so, just add

    public static string GetContentFromSPList(string cValueToFind)
    {   
           string cValueFound = "";
            try
            {
               //code
            }
            catch (Exception ex)
            {
            }
            finally
            {
               //some cleanup
            }
    
            return cValueFound ;
     }