0

I´m working on Web API to give permissions a public SharePoint online website, so I call list like these

 public List<MenuPrincipal> GetMenuOptions(string idioma)
        {
            List<MenuPrincipal> result = new List<MenuPrincipal>();
            try
            {
                SecureString pwd = SPAccess.GetPasswordFromConsoleInput(Definitions.UserPassword);

                using (var context = new ClientContext(Definitions.WebUrl))
                {
                    context.Credentials = new SharePointOnlineCredentials(Definitions.UserName, pwd);
                    List listResult = context.Web.Lists.GetByTitle("MenuPrincipal");



                    CamlQuery query = new CamlQuery();
                    query.ViewXml = string.Format("<View><Query><Where><Eq><FieldRef Name='Idioma'/><Value Type='Text'>{0}</Value></Eq></Where></Query><RowLimit>1000</RowLimit></View>", idioma);
                    ListItemCollection items = listResult.GetItems(query);

                    context.Load(items);
                    context.ExecuteQuery();

                    foreach (ListItem item in items)
                    {
                        result.Add(new MenuPrincipal
                        {
                            Titile = item["Title"] == null ? string.Empty : item["Title"].ToString(),
                            Enlace = item["Enlace"] == null ? string.Empty : item["Enlace"].ToString(),
                            Posicion = item["Posicion"] == null ? 0 : Convert.ToInt32(item["Posicion"]),
                            Idioma = item["Idioma"] == null ? string.Empty : item["Idioma"].ToString(),
                            Funcion = item["Funcion"] == null ? string.Empty : item["Funcion"].ToString()
                        });
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return result.OrderBy(x => x.Posicion).ToList();
        }

It works fine, but the speed of my page is very low now. The problem is in my foreach, it calls alot of values, and I only five of these, how can I achieve that call only values I have into my foreach instead all other ones.

There is picture of problem: enter image description here enter image description here Help is very appreciated Regards


Update

As I can see query gets a list of 5 arrays, and each one has "56" values, why is it happening? enter image description here enter image description here I read something in MSDN to change my context.Loadlike this:

       context.Load(
                items,
                    lists => lists.Include(
                    list => list.Title,
                    list => list.Enlace,
                    list => list.Posicion,
                    list => list.Idioma,
                    list => list.Funcion));

But I can´t retrieve value from list so all marked in red like this:

enter image description here

There is MSDN link

0

I think you are confusing two things here: ListItems and Fields.

In one of your screenshots, you can clearly see that the number of list items in your list item collection items that is returned form your query is 5. It says items Count=5.

That means that the foreach is only looping over 5 items.

Where you see the 56 is in each item, as field values. That means each list item you get has 56 field (column) values with it. But, you are not looping over 56 field values, or even touching most of them, you are only working with (coincidentally) 5 fields: Title, Enlace, Posicion, Idioma and Funcion.

So your slow page performance is not due to looping over many items. It must be because of something else.

As I can see query gets a list of 5 arrays, and each one have "56" values, why it happening?

Right, so your query returns 5 items. If there are 56 field values, it means that the list you are getting the items from has many, many columns on it. Some of those field values are probably hidden fields for internal SharePoint use, so we can't say that your list has 56 columns. But it probably has a lot of columns, and you are not telling SharePoint which specific fields/columns you want on each item, so it's just sending them all back to you.

Now, if you want to retrieve the list items, and not bring back all 56 field values, but only bring back the 5 field values you want to work with, you need to use ViewFields in your query to specify exactly which field values you want returned, like this:

string queryString = "<View>";
queryString += string.Format("<Query><Where><Eq><FieldRef Name='Idioma'/><Value Type='Text'>{0}</Value></Eq></Where></Query><RowLimit>1000</RowLimit>", idioma);
queryString += "<ViewFields>";
queryString += "<FieldRef Name='Title' />";
queryString += "<FieldRef Name='Enlace' />";
queryString += "<FieldRef Name='Posicison' />";
queryString += "<FieldRef Name='Idioma' />";
queryString += "<FieldRef Name='Funcion' />";
queryString += "</ViewFields></View>";

CamlQuery query = new CamlQuery();
query.ViewXml = queryString;
ListItemCollection items = listResult.GetItems(query);

This might help a little bit because it means much less data is being transferred from the server when you make your query.

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.