Thursday, November 24, 2016

How to Enable SharePoint JSOM IntelliSense in Visual Studio

JSOM is a part of SharePoint Client Object Model that intended for accessing and manipulating SharePoint objects by using JavaScript in an asynchronous. It’s provides a comprehensive set of APIs that can be used to perform operations on most SharePoint objects such as Site, Web, List, items, Content Types, User Permission and so forth.

To Enable SharePoint JSOM IntelliSense in Visual Studio, you shloud add reference directive file to your project as the following:

/// <reference path="~/_layouts/15/init.js" /> 
/// <reference path="~/_layouts/15/SP.Core.js" /> 
/// <reference path="~/_layouts/15/SP.Runtime.js" /> 
/// <reference path="~/_layouts/15/SP.UI.Dialog.js" /> 
/// <reference path="~/_layouts/15/SP.js" />

Monday, November 21, 2016

Dislike SharePoint List Item using CSOM

ListItem listItem = //get list item
ctx.Load(listItem, i => i["LikedBy"]);
ctx.ExecuteQuery();

var users = listItem["LikedBy"] as FieldUserValue[];
listItem["LikedBy"] = users.Where(x => x.LookupValue != "User Name");
listItem.Update();

ctx.ExecuteQuery();

Retrieving list items from a specific view using CSOM

using (ClientContext ctx = new ClientContext("https://sp/test/"))
            {
                Web web = ctx.Web;
                List list = web.Lists.GetByTitle("test");
                ctx.Load(list);
                ctx.ExecuteQuery();

                View view = list.Views.GetByTitle("All Items");
                ctx.Load(view);
                ctx.ExecuteQuery();

                CamlQuery query = new CamlQuery();
                query.ViewXml = view.ViewQuery;

                ListItemCollection listItems = list.GetItems(query);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
            }

Read a Text File One Line at a Time to the List (C#)

This example reads the contents of a text file, one line at a time, into a string using the ReadLine method of the StreamReader class. Each text line is stored into the string line and added to the list.

public static List<string> ReadFromFile(string path)
        {
            List<string> lineList = new List<string>();
            using (StreamReader reader = new StreamReader(path))
            {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    lineList.Add(line);
                }

            }
            return lineList;
        }