.net 客户端对象模型读取方法汇总,今后当字典用

static ClientContext _clientContext;
        static Web _web;
        static List _list;
        static ListItem _listItem;
        static ListItemCollection _listItemCollection;
        static CamlQuery _query;
        static FieldLookupValue _lookupValule;
        static Field _field;
        static FieldChoice _fieldChose;

        static void Main(string[] args)
        {
            //putOutSiteTitle();
            //putOutList();
            //putOutPartialWeb();
            //outPutListItems();
            //outPutCamlListItems();
            //outPutCamlPartialListItems();
            //outPutCamlPartialUpdateListItems();
            //GetSetValue.putDateTime();
            //GetSetValue.putUserType();
            //putChoice();
            //putSPChoice();
            CamlJoin.GetList();
            Console.Read();
        }

        #region Title
        static void putOutSiteTitle()
        {
            _clientContext = new ClientContext("http://moss:888");
            _web = _clientContext.Web;
            _clientContext.Load(_web);
            _clientContext.ExecuteQuery();
            Console.WriteLine("Web Title:{0},Web Id:{0}", _web.Title, _web.Id);
        }
        #endregion

        #region List
        static void putOutList()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("Demo");
            _clientContext.Load(_list);
            //Console.WriteLine("ExecuteQuery Before");
            //Console.WriteLine("List Id:{0} List Title:{1}", _list.Id, _list.Title);
            _clientContext.ExecuteQuery();
            Console.WriteLine("ExecuteQuery After");
            Console.WriteLine("List Id:{0} List Title:{1}", _list.Id, _list.Title);
        }
        #endregion

        #region  Partial Web Info
        static void putOutPartialWeb()
        {
            _clientContext = new ClientContext("http://moss:888");
            _web = _clientContext.Web;
            _clientContext.Load(_web,
                s => s.Title,
                s => s.Description);
            _clientContext.ExecuteQuery();
            Console.WriteLine("Web Title:{0},Web Descrition:{0}", _web.Title, _web.Description);
            //Console.WriteLine("Web Id:{0}", _web.Id);
        }
        #endregion

        #region List Items
        static void outPutListItems()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("MetadateTest");
            _query = new CamlQuery();
            _listItemCollection = _list.GetItems(_query);
            _clientContext.Load(_listItemCollection);
            _clientContext.ExecuteQuery();
            Console.WriteLine("Demo List Search");
            foreach (ListItem item in _listItemCollection)
            {
                _lookupValule = item.FieldValues["Lookup"] as FieldLookupValue;
                if (_lookupValule != null)
                {
                    Console.WriteLine("Title:{0},Lookup:{1},技术:{2}", item["Title"], _lookupValule.LookupValue, item["_x6280__x672f_"]);
                    Console.WriteLine();
                }
            }

        }
        #endregion

        #region Caml List Item
        static void outPutCamlListItems()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("MetadateTest");
            
            _query = new CamlQuery();
            _query.ViewXml = string.Concat(
                "<View>",
                    "<Query>",
                        "<Where>",
                            "<BeginsWith>",
                                "<FieldRef Name='_x6280__x672f_'/>",
                                "<Value Type='Text'>SharePoint</Value>",
                            "</BeginsWith>",
                        "</Where>",
                    "</Query>",
                "</View>"
                );

            _field = _list.Fields.GetByTitle("技术");
            _listItemCollection = _list.GetItems(_query);
            _clientContext.Load(_listItemCollection);
            _clientContext.Load(_field);
            _clientContext.ExecuteQuery();

            Console.WriteLine("Caml BeginsWith SharePoint");
            foreach (ListItem item in _listItemCollection)
            {
                Console.WriteLine("Title:{0},Lookup:{1},技术:{2}", item["Title"], item["Lookup"], item[_field.InternalName]);
                Console.WriteLine();
            }
        }
        #endregion

        #region Caml Partial List Item
        static void outPutCamlPartialListItems()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("MetadateTest");
            _query = new CamlQuery();
            _query.ViewXml = string.Concat(
                "<View>",
                    "<Query>",
                        "<Where>",
                            "<BeginsWith>",
                                "<FieldRef Name='_x6280__x672f_'/>",
                                "<Value Type='Text'>SharePoint</Value>",
                            "</BeginsWith>",
                        "</Where>",
                    "</Query>",
                "</View>"
                );

            _listItemCollection = _list.GetItems(_query);
            _clientContext.Load(_listItemCollection, itemCollection => itemCollection.Include(
                                                  item => item["Title"],
                                                  item => item["Lookup"],
                                                  item => item["_x6280__x672f_"])
                                 );
            _clientContext.ExecuteQuery();
            Console.WriteLine("Caml Partial BeginsWith SharePoint");
            foreach (ListItem item in _listItemCollection)
            {
                Console.WriteLine("Title:{0},Lookup:{1},技术:{2}", item["Title"], item["Lookup"], item["_x6280__x672f_"]);
                Console.WriteLine();
            }
        }
        #endregion

        #region Upadate
        static void outPutCamlPartialUpdateListItems()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("MetadateTest");
            _query = new CamlQuery();
            _query.ViewXml = string.Concat(
                "<View>",
                    "<Query>",
                        "<Where>",
                            "<BeginsWith>",
                                "<FieldRef Name='_x6280__x672f_'/>",
                                "<Value Type='Text'>SharePoint</Value>",
                            "</BeginsWith>",
                        "</Where>",
                    "</Query>",
                "</View>"
                );

            _listItemCollection = _list.GetItems(_query);
            _clientContext.Load(_listItemCollection, itemCollection => itemCollection.Include(
                                                  item => item["Title"],
                                                  item => item["Lookup"],
                                                  item => item["_x6280__x672f_"])
                                 );
            _clientContext.ExecuteQuery();
            Console.WriteLine("Caml Partial BeginsWith SharePoint Update Before");
            foreach (ListItem item in _listItemCollection)
            {
                Console.WriteLine("Title:{0},Lookup:{1},技术:{2}", item["Title"], item["Lookup"], item["_x6280__x672f_"]);
                Console.WriteLine();
                item["Title"] = DateTime.Now;
                item.Update();
            }
            _clientContext.ExecuteQuery();
            Console.WriteLine("Caml Partial BeginsWith SharePoint Update after");
            foreach (ListItem item in _listItemCollection)
            {
                Console.WriteLine("Title:{0},Lookup:{1},技术:{2}", item["Title"], item["Lookup"], item["_x6280__x672f_"]);
                Console.WriteLine();
                //item.DeleteObject();
            }

            //_list.DeleteObject();

        }
        #endregion

        #region Choice
        static void putChoice()
        {
            _clientContext = new ClientContext("http://moss:888");
            _list = _clientContext.Web.Lists.GetByTitle("Custom");
            _fieldChose = _clientContext.CastTo < FieldChoice > (_list.Fields.GetByInternalNameOrTitle("选项类型"));
            _clientContext.Load(_fieldChose);
            _clientContext.ExecuteQuery();
            foreach (string choice in _fieldChose.Choices)
            { 
                Console.WriteLine(choice);
            }
        }

        static void putSPChoice()
        {
            using (SPSite site = new SPSite("http://moss:888"))
            {
                using (SPWeb web = site.RootWeb)
                { 
                    SPList list=web.Lists["Custom"];
                    SPField field = list.Fields.GetField("选项类型");
                    SPFieldChoice choice = list.Fields.GetField(field.InternalName) as SPFieldChoice;
                    foreach (string c in choice.Choices)
                    {
                        Console.WriteLine(c);
                    }
                }
            }
        }
        #endregion

 

posted @ 2012-06-18 18:00  @@@一统@@@  阅读(380)  评论(1编辑  收藏  举报