linq and rest api in sharepoint

 

  //1.make sure your application using the .net fromwork 3.5
        //2.create entity classes using the instruction:spmetal /web:http://sv1:82 /namespace:MyLinq /code:c:\MyLinq.cs,
        //and copy the MyLinq.cs file to your project
        static void linq()
        {
            string url = "http://sv1:82";
            using (MyLinq.MyLinqDataContext context = new MyLinq.MyLinqDataContext(url))
            {

                var r = from x in context.PaySlip
                        where x.名称.Length > 0
                        select x;
                // select new {x.名称,x.路径,x.文档创建者,x.标题};
                Console.WriteLine(r.FirstOrDefault().标题);

            }
        }

        //1.add a service reference to http://sv1:82/_vti_bin/listdata.svc
        static void Rest()
        {
            string url = "http://sv1:82/_vti_bin/listdata.svc";
            ServiceReference1.HomeDataContext h1 = new ServiceReference1.HomeDataContext(new Uri(url));
            h1.Credentials = new System.Net.NetworkCredential("administrator", "Abcd1234", "sk");
            var list = h1.PaySlip.ToList();
            if (list.Count() > 0)
            {
                Console.WriteLine(list[0].标题);
            }
            else
            {
                Console.WriteLine("List is empty");
            }
        
        }




 


//client object model

string url = "http://abc.abc.com.cn/LeaveApplication/"; ClientContext clientContext = new ClientContext(url); clientContext.Credentials = new System.Net.NetworkCredential("spsadmin", "abc123", "domain"); Site siteCollection = clientContext.Site; Web site = clientContext.Web; List list = site.Lists.GetByTitle("Application List"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View/>"; ListItemCollection collection = list.GetItems(camlQuery); clientContext.Load(list); clientContext.Load(collection); clientContext.ExecuteQuery(); Console.WriteLine(collection.Count.ToString()); var item = collection[0]; FieldLookupValue o = item["Leave_x0020_Type"] as FieldLookupValue; //FieldLookup fieldLookup = list.Fields.GetByTitle("Leave_x0020_Type") as FieldLookup; //FieldLookupValue lookUpValue1 = field collection[0]["Leave_x0020_Type"] // userMaintenanceCenterId = lookUpValue1.LookupValue; Console.Read();

 

        static void ClientModel()
        {
            string url = "http://sv1:82";
            Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(url);
            var site = context.Web;
            context.Load(site);
            context.ExecuteQuery();
            Console.WriteLine(site.Title);
        }


        //make sure you started the " SharePoint Foundation Search service" on the page "Services on Server"
        //reference Microsoft.SharePoint.Search
        //http://www.sharepointblues.com/2010/02/15/content-queries-using-sharepoint-enterprise-search-api/
        static void fullTeextSqlQuery()
        {
            string url="http://sv1:82/";
            Microsoft.SharePoint.SPSite site = new Microsoft.SharePoint.SPSite(url);
            Microsoft.SharePoint.Search.Query.FullTextSqlQuery qry = new Microsoft.SharePoint.Search.Query.FullTextSqlQuery(site);
            qry.ResultTypes = Microsoft.SharePoint.Search.Query.ResultType.RelevantResults;
            string sql = "select title,author,path from scope() where author='administrator'";
            qry.QueryText = "select title from scope()";
            var result=qry.Execute();
            Console.WriteLine(result.Count.ToString());

        }

 

 

 

 

 

posted @ 2013-09-22 16:17  zyip  阅读(316)  评论(0编辑  收藏  举报