1

I have a problem when I need to retrieve data via REST from SharePoint 2010 list. In the returned data Attachments and user / group column is not contains data. I do not know if it's because it's collections. I have written a very simple code example and hope that there is someone who can help.

Edit 26/9-2016 since no one has answered this question, Is there someone who knows if it is possible is to get data into C# code, I can see the information I need in a Browser, but with what do I call this with (return object) , so I get data out in some kind of the structure/object?

DataServiceContext( new Uri(http://SPSite.net/sites/testsite/_vti_bin/listdata.svc/Lycklist(1)/Attachments/)

enter code here

public static class REST
    {
        public static LyckDataContext GetContext()
        {
            LyckDataContext ctx = new LyckDataContext(new Uri("http://SPSite.net/sites/testsite/_vti_bin/listdata.svc"))
            {Credentials =  CredentialCache.DefaultNetworkCredentials};
                  return ctx;
        }


        public static List<MyItems> GetMyList()
        {
            LyckDataContext ctx = GetContext();

            List<MyItems> ItemList = new List<MyItems>();
            var myList = (from item in ctx.Lycklist select item) as DataServiceQuery<LyckData.LycklistItem>;
            // This depends on the client, if callback is needed.  
            // IAsyncResult res = myList.BeginExecute(GetResult, myList);
            try
            {
                foreach (var item in myList)
                {
                    MyItems newItem = new MyItems();
                    string allUsers = "";
                    if (item.Usersandgroups.Count > 0) // There is never any users??
                    {
                        foreach (var user in item.Usersandgroups)
                        {
                            allUsers += user.Name;
                        }
                        newItem.Users = allUsers;
                    }
                    if (item.Attachments.Count > 0) //There is also no attachment
                    {
                        foreach (var att in item.Attachments)
                        {
                            string temp = att.Name;
                        }
                    }
                    //There are no problems with strings
                    newItem.Title = item.Title;
                    newItem.Name = item.Mybodytekst;

                    ItemList.Add(newItem);
                }
            }
            catch (Exception ex)
            { string message = ex.Message; }


            return ItemList;
        }

        private static void GetResult(IAsyncResult result)
        {
            DataServiceQuery<LycklistItem> query = (DataServiceQuery<LycklistItem>)result.AsyncState;

            foreach (LycklistItem Item in query.EndExecute(result))
            {
                //code
            }
        }
    }      
        public class MyItems
    {        
        //Democlass
       public string Users { get;set; }
       public string Title { get; set; }
       public string Name { get; set; }
       public string AttachmentsPath { get; set; }
    }

1 Answer 1

0

You can get Attachments using $expand operator. $expand is needed for __deferred properties, lookup columns and User/Group columns.

For all item use following end-point

_vti_bin/listdata.svc/Lycklist?$expand=Attachments

For single item

_vti_bin/listdata.svc/Lycklist(1)?$expand=Attachments

enter image description here

You can use my SharePoint REST Client to test/explore REST API.

3
  • Hi Atish, This is really good, but how do I get this output into my C # code, because I'm forced to send Credentials with. Therefore, I have created a web service and getting data into a DataServiceContext. I can see all data with Rest Client. Sep 26, 2016 at 16:04
  • You have to change current C# code to bind Json with C# object Sep 26, 2016 at 16:15
  • Hi @Atish Yes, but how do I authenticate and how do i make the call to the service from the code? EX var response = ?? ---- I have searched the web and there are no examples of how it's done. there are a lot of small JavaScript snippet, but I need it in my backend code. Thank for you bother to spend the time, I am very grateful. Sep 26, 2016 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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