惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - 吴东雷

配置NuGet自动下载丢失的Dll SharePoint2010站点在IE下的操作异常 ExtJS的store.sync向Asp.net MVC的Action提交时引发System.Reflection.AmbiguousMatchException异常 ReportViewerWebWebpart显示报表时显示的脚本错误:Uncaught Sys.ArgumentNullException: Sys.ArgumentNullException: 值不能为 null。参数名: panelsCreated[0] 【转载】javascript getComputedStyle,getPropertyValue,CurrentStyle说明 【原创】关于SPSecurity.RunWithElevatedPrivileges的一个问题[A problem about SPSecurity.RunWithElevatedPrivileges] 【半原创】SharePoint CAML 查询生成器[SharePoint 2010 CAML Query Builder] 【原创】在DataFormWebPart中将列表附件显示为图片(二)[How to display list item attachments as image in DFWP Part 2] 【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP] 【原创】SharePoint2010的SaveButton如何实现在跳转前给用户以提示[How to implement SaveButton display an alert when Redirect action] 【原创】在DataFormWebPart中将列表附件显示为图片(一)[How to display list item attachments as image in DFWP Part1] Asp.net中全局缓存的几种方式 修改Model中hasMany中自动生成的属性值 ExtJS中自定义组件并将事件暴露给调用者 从控制台测试SharePoint遇到的两个问题 The Sencha Class System(理解ExtJs定义类的过程) IIS7中Sites(站点), Applications(应用程序), Virtual Directories(虚拟目录)的粗浅理解 按钮的ajax请求时,一次点击两次提交的问题 MVC3中Razor模板引擎如何修改View的基类
【原创】如何让SharePoint2010的内联代码支持.Net framework ...
吴东雷 · 2012-09-06 · via 博客园 - 吴东雷

SharePoint 2010是建立在.Net framework 3.5基础上的,所以其必然要支持3.5的语法。在SharePoint开发过程中,对于系统的实现可以有多种方法,其中之一是不被微软所提倡的内联代码。所谓内联代码就是直接在aspx中编写服务器端代码,其介绍文章在网上有很多,例如:http://blog.pixelmill.com/1037/sharepoint-2010-allow-server-side-code-inline-code-blocks/http://www.hexanes.com/?p=408等等。但是,这种嵌入的代码只支持2.0的语法,所以像Linq、var等都不能支持,想要支持的话,需要修改网站的web.config。

1、修改compilation节中的配置,例如我将其修改为:

    <compilation batch="false" debug="true" optimizeCompilations="true">
      <assemblies>
        <add assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.SharePoint.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>        
      </assemblies>
.......
</compilation>

这里需要说明的是,如果在内联代码中,还要调用其它的类库(例如自己写的或者第3方的),也可以通过这种方式加入到这里(好像放在Bin目录也是可以的,但各有利弊吧)。

2、加入system.codedom节配置:在configuration节中加入以下配置

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,  
                    Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5" />
        <providerOption name="WarnAsError" value="false" />
      </compiler>
    </compilers>
  </system.codedom>  

这样就可以在内联代码中编写.net framework 3.5的代码了,如在我的页面中:

<%@ Page Language="C#" masterpagefile="~masterurl/default.master" title="areaTrend" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="xxxx.Web.Security" %>
<%@ Import Namespace="xxxx.Web.Security.Model" %>
<%@ Import Namespace="xxxx.SPLibraryExtend.Utils" %>
<%@ Import Namespace="xxxx.SPLibraryExtend" %>

<script type="text/c#" runat="server">        
    void Page_Load(object sender, System.EventArgs e) 
    {
        
    } 
</script>
<asp:Content ID="ContentTitle" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle">
    xxxxxxxxx</asp:Content>
<asp:Content ID="ContentPageHead" runat="server" ContentPlaceHolderID="PlaceHolderAdditionalPageHead">
    <link rel="stylesheet" type="text/css" href="/Style Library/common/style/xxxx.css" />
    <style type="text/css">
        .infoList_dept li { width:270px; float:left; margin-left:25px; line-height:28px;}
    </style>
</asp:Content>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
<div class="grid_12">
<div class="border_gray" >
    <div class="title_orange">
        <div class="dot_orange"></div>
        <div class="title_text"></div>
    </div>
    <div class="padding_20">
    <!--内容开始-->
 
        <%
            SqlRoleExProvider role=null;
            foreach (var provider in Roles.Providers)
            {
                role = provider as SqlRoleExProvider;
                if (role!=null)
                {
                    break;
                }
            }
                    
            List<RoleExModel> lstParent = new List<RoleExModel>();
            
            lstParent.Add(role.GetRoleExModelByName("分类1"));
            lstParent.Add(role.GetRoleExModelByName("分类2"));
            lstParent.Add(role.GetRoleExModelByName("分类3"));
            foreach (var p in lstParent)
            {
        %><div class="title_white_orange">
          <div class="dot_orange"></div>
                    <div class="title_text"><%=p.RoleName %></div>
                </div>
                <div class="empty_5"></div>
                <ul class="infoList_dept">
        <%
                List<RoleExModel> lstChild = (List<RoleExModel>)role.getRoleExModelsByParentId(p.RoleId).OrderBy(t => t.Sequence).ToList();                
                foreach (var c in lstChild)
                {
        %><li><a href="/xxxx.aspx?DeptGuid=<%=c.RoleId %>&DeptId=<%=FBARoleHelper.GetFBARoleId(c.RoleName) %>" target="_blank"><%=c.RoleName %></a></li>
        <%
                }
        %></ul>
                <div class="clear"></div>
                <div class="empty_20"></div>
        <%                    
            }
        %><!--内容结束--></div>
</div></div>
</asp:Content>

一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。