zoukankan      html  css  js  c++  java
  • Linq to sharepoint

     

     一、Linq to SharePoint

    首先Linq to SharePoint编程语言 C# 和 Microsoft Visual Basic .NET 的一个功能,编译器是 Visual Studio 附带的。

    Linq to SharePoint是LINQ家族中的一员,如果使用过例如Linq to SQL的话,就会发现Linq to SharePoint与其有很多相同之处。

    如Linq to SQL中有DataContext类,用来打开数据库连接并进行操作。Linq to SharePoint也有DataContext,同样用来对SharePoint中的数据进行操作。

    二、SPMetal

    image
     

    使用Linq to SharePoint 很重要的一步是生成Entity类,也就是所说的SharePoint实体类,用来映射到SharePoint对应的文档库、列表等等。手动创建实体类,是一件很痛苦的事情,复杂且易错,幸好有SPMetal工具来自动生成。当然,这个工具也有一些限制,如自定义字段不会生成对应实体,文档库附件不会生成。如果有这样的需求,那只能使用扩展对象关系映射,这一点上本人也涉猎较浅,就不做赘述,还是直接使用微软提供的工具吧。接下来是SPMetal的使用方法:

    1、使用默认代码生成规则

    如果没有特殊要求,默认规则就可以满足程序的要求。

    步骤一、打开命令行CMD

    步骤二、cd(空格)%ProgramFiles%Common FilesMicrosoft Sharedweb server extensions15(SharePoint 2010则替换为12)BIN 

    步骤三、运行命令:

    SPMetal /web:http://yoursite /code:类名.cs /language:csharp回车
    Warning:All content type for list 列名 were excluded.
    SPMetal /web:http://我们的SharePoint站点:端口 /code:SiteEntities.cs
     

    注意:/号前空格

    还有一些其他的参数,可以设置生成代码的namespace、serializetion,可以参考MSDN文档
     
    下面以此List为对象进行相应的增,删,改操作

    1、添加操作

    复制代码
                
                    EntitiesDataContext edc = new EntitiesDataContext(SPContext.Current.Web.Url);                
                    PersonsItem installitem = new PersonsItem();
                    installitem.Title = “张三”;
                    edc.Persons.InsertOnSubmit(installitem);
                    edc.SubmitChanges();
      
    复制代码

    2、修改操作

    复制代码          
              

               EntitiesDataContext edc=new EntitiesDataContext(SPContext.Current.Web.Url);
                  var mypersons = edc.Persons;//列名
                  var updateitem = (from item in mypersons
                                    where item.Title == "张三"
                                    select item).First();
                  updateitem.Title = "李四";
                  edc.SubmitChanges();

     
    复制代码

    3、删除操作

    复制代码
                
                  EntitiesDataContext edc=new EntitiesDataContext(SPContext.Current.Web.Url);
                  var mypersons = edc.Persons;//列名
                  var deleteitem = (from item in mypersons
                                    where item.Title == "张三"
                                    select item).First();
                  updateitem.Title = "李四";
                  edc.SubmitChanges();
                // Deleting the list item           
                  mypersons.DeleteOnSubmit(delItem);
                // Submit the changes            
                edc.SubmitChanges();   
    复制代码
     
  • 相关阅读:
    docker容器安装使用
    hashMap学习
    spark运行方式及其常用参数
    java面试题基础
    大数据面试题
    java面试题
    Java四种线程池
    大数据
    pyspark 日期格式
    CMake error:System Error:No such file or directory CMake error:Could not open file for write in copy operation xxxx.ros_Config.cmake.tmp.
  • 原文地址:https://www.cnblogs.com/dmyao/p/7090819.html
Copyright © 2011-2022 走看看