locked
how to return collection type with async method RRS feed

  • Question

  • Hi 

    I need to return 'SPitems' list from async method that access SharePoint list via Atom xml.  to assigning values to sampledatasource.cs i need to return this SPitem list from async method. any help greatly appreciated.. following is my method 

    public async void BindData()
            {
                string restUrl = "http://EG/EHL/legal/_api/web/lists/getByTitle('Pictures')/items?$select=ID,Title,FileRef,ImageWidth,ImageHeight,Last_x0020_Modified,Category";
                HttpClientHandler httpClientHandler = new HttpClientHandler();
                httpClientHandler.UseDefaultCredentials = true;

                HttpClient client = new HttpClient(httpClientHandler);
                client.DefaultRequestHeaders.Add("Accept", "application/atom+xml");
                client.DefaultRequestHeaders.Add("ContentType", "application/atom+xml;type=entry");

                var response = await client.GetAsync(restUrl);
                response.EnsureSuccessStatusCode();

                string responseBody = await response.Content.ReadAsStringAsync();
                System.IO.StringReader reader = new StringReader(responseBody);
                XDocument responseXml = XDocument.Load(reader, LoadOptions.None);

                XNamespace atom = "http://www.w3.org/2005/Atom";
                XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
                XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

                var SPitems = from item in responseXml.Descendants(atom + "entry")
                              select new
                              {

                                  Title = item.Element(atom + "content").Element(m + "properties").Element(d + "Title").Value,
                                  Image = string.Format("http://EG{0}", item.Element(atom + "content").Element(m + "properties").Element(d + "FileRef").Value),
                                  Category = item.Element(atom + "content").Element(m + "properties").Element(d + "Category").Value,
                                  Subtitle = item.Element(atom + "content").Element(m + "properties").Element(d + "Last_x0020_Modified").Value
                                  // DueDate = DateTime.Parse(item.Element(atom + "content").Element(m + "properties").Element(d + "DueDate").Value)
                                  //Status = item.Element(atom + "content").Element(m + "properties").Element(d + "Status").Value,
                                  //PercentComplete = decimal.Parse(item.Element(atom + "content").Element(m + "properties").Element(d + "PercentComplete").Value)
                              };



                //need to return  SPitems;
                
            }

    Regards

    Radika

    Saturday, September 21, 2013 10:02 AM

Answers

  • Hi Radika,

    try it like this:

    public async Task<SPitems> BindData()
            {
    
              // your code here
    
              return  spItems;
                
            } 

    Edit: Ah, I've seen it's an anonymous type. You should create a class for the items and use an collection for them. The return-type of the method can't be an anonymous one. In the code-snipped above SPitems must be a class in the Method definition.

    So this is what it could look like:

            public async Task<IEnumerable<SPItem>> BindData()
            {
                List<SPItem> spItems = (yourLINQquery with select new SPItem{Title=...}).ToList();
                return spItems;
            }
    
    
    // plus somewhere else the class definition
    public  class SPItem
        {
            public string Title { get; set; }
            //...
        }


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook



    Saturday, September 21, 2013 11:32 AM
  • you can await again on this new method:

    so myCollection = await BindData()


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Radika Tuesday, September 24, 2013 5:06 AM
    Monday, September 23, 2013 7:51 AM

All replies

  • Hi Radika,

    try it like this:

    public async Task<SPitems> BindData()
            {
    
              // your code here
    
              return  spItems;
                
            } 

    Edit: Ah, I've seen it's an anonymous type. You should create a class for the items and use an collection for them. The return-type of the method can't be an anonymous one. In the code-snipped above SPitems must be a class in the Method definition.

    So this is what it could look like:

            public async Task<IEnumerable<SPItem>> BindData()
            {
                List<SPItem> spItems = (yourLINQquery with select new SPItem{Title=...}).ToList();
                return spItems;
            }
    
    
    // plus somewhere else the class definition
    public  class SPItem
        {
            public string Title { get; set; }
            //...
        }


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook



    Saturday, September 21, 2013 11:32 AM
  • Hi thomas

    Thanks for reply. one thing need  how to call this and assume return seem's we cant normally assigning with calling the method since this returns as Task. please kindly give how to call this and assume return values

    Regards

    Radika

    Monday, September 23, 2013 5:19 AM
  • you can await again on this new method:

    so myCollection = await BindData()


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Radika Tuesday, September 24, 2013 5:06 AM
    Monday, September 23, 2013 7:51 AM
  • Hi got it

    Thanks for help \

    Regards

    Radika

    Tuesday, September 24, 2013 5:05 AM