Thursday, May 29, 2014

Get Choice Field values using Client Side Object Model in SharePoint 2013

Folks,

In this post i will explain you how to retrieve choice field values in Client Side Object Model (CSOM) in SharePoint 2013. I have tested this code in Auto hosted app on SharePoint online environment. You can use this code snippet to get values from choice field for custom filter and search conditions.

 var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                List queslist = clientContext.Web.Lists.GetByTitle("YourListTitle");
                Field choice = queslist.Fields.GetByInternalNameOrTitle("FieldName");

                FieldChoice fldChoice = clientContext.CastTo<FieldChoice>(choice);
                clientContext.Load(fldChoice, f => f.Choices);
                clientContext.ExecuteQuery();
                foreach (string  item in fldChoice.Choices)
                {
                    ddltopic.Items.Add(item.ToString());
//add choices to dropdown list
                }
}

You have to get the SharePoint context, then get the list field and then get that field choice and use the choice fields as per your needs, i have added the choice values in a drop-down list.

Let me know if you face any problem..

Happy Coding :)

Friday, May 9, 2014

Search SharePoint List items using jQuery/JavaScript and Web Service

You can use jquery/javascript to search items from a SharePoint list. You can define you CAML query and based on that result will be displayed. Lists.asmx web service is used in this case. You can use this to search items both on SharePoint online or on-premises versions.

List.asmx service is called and post method is called provided with soap envelope and in return we get the list items in from of xml. You can also highlight the searched keyword in the result.