设为首页 收藏本站
查看: 695|回复: 0

[经验分享] 让 SharePoint 支持 .NET 3.5

[复制链接]

尚未签到

发表于 2015-9-25 08:56:41 | 显示全部楼层 |阅读模式
  SharePoint 2007 是基于 .NET 2.0 开发,那 SharePoint 2007 支持 .NET 3.5 嘛? 答案是肯定的!任何 .NET 2.0 开发的程序包括都可以运行在 .NET 3.5 之上,可以充分利用 .NET 3.5 的新特性,包括 C# 3.5 的 LINQ,ASP.NET AJAX 等。
  好吧,你说支持,那么我们就写一段 C#3.5 的代码吧:

  1. 建一个 Document Library: BasicPages,Template 选择 Basic Page
  2. 在 BasicPages 中New一新页面:NET35Test.aspx
  3. 在 SharePoint Designer 打开
  4. 贴入文末的代码

  5. 按 F12 预览

  6. 得到如下错误
  Compiler Error Message: CS1526: A new expression requires () or [] after type

  注意:如果你得到的是 Parser Error Message: Code blocks are not allowed in this file.,你可以先参考:
ERROR: Code blocks are not allowed in this file(此文件中不允许代码块) - SharePoint 2007


  到底支不支持啊?稍安勿躁,让我们往 web.config 添加如下配置节:


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

  
  重新刷新,得到如下错误,
  Compiler Error Message: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)

  Line 5: <%@ Import Namespace="System.Linq" %>  

  LINQ 来了,看来有戏,继续往 web.config 添加如下配置节:onfiguration>
<system.web>
<compilation batch="false" debug="false">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
</compilation>
</system.web>
</configuration>

<configuration>
<system.web>
<compilation batch="false" debug="false">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
</compilation>
</system.web>
</configuration>  
  重新刷新,啊哈结果出来:

DSC0000.png

  测试页面代码:

DSC0001.gif DSC0002.gif Code
<%-- _lcid="1033" _version="12.0.4518" _dal="1" --%>
<%-- _LocalBinding --%>
<%@ Page language="C#" MasterPageFile="~masterurl/default.master"    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>

<script type="text/c#" runat="server">

    /// <summary>
    /// 示例改编自MSDN:
    /// 如何:查询对象集合(C# 编程指南)
    /// http://msdn.microsoft.com/zh-cn/library/bb907066.aspx
    /// </summary>
    public class StudentClass
    {
        #region data

        protected enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };

        protected class Student
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int ID { get; set; }
            public GradeLevel Year;
            public List<int> ExamScores;
        }

        protected static List<Student> students = new List<Student>
        {
            new Student {FirstName = "Terry", LastName = "Adams", ID = 120, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 99, 82, 81, 79}},
            new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, Year = GradeLevel.ThirdYear,ExamScores = new List<int>{ 99, 86, 90, 94}},
            new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 93, 92, 80, 87}},
            new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, Year = GradeLevel.FourthYear,ExamScores = new List<int>{ 97, 89, 85, 82}},
            new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 35, 72, 91, 70}},
            new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 92, 90, 83, 78}},
            new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 88, 94, 65, 91}},
            new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 75, 84, 91, 39}},
            new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, Year = GradeLevel.SecondYear, ExamScores = new List<int>{ 97, 92, 81, 60}},
            new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, Year = GradeLevel.ThirdYear, ExamScores = new List<int>{ 68, 79, 88, 92}},
            new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, Year = GradeLevel.FirstYear, ExamScores = new List<int>{ 94, 92, 91, 91}},
            new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, Year = GradeLevel.FourthYear, ExamScores = new List<int>{ 96, 85, 91, 60}}
        };

        #endregion

        //Helper method
        protected static int GetPercentile(Student s)
        {
            double avg = s.ExamScores.Average();
            return avg > 0 ? (int)avg / 10 : 0;
        }

        public string QueryHighScores(int exam, int score)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<pre style='font-size: 21px;'>");
            sb.Append("Hello MOSS 2007. You're running on .NET 3.5.\n\nPopulate students via LINQ:\n");
            var highScores = from student in students
                             where student.ExamScores[exam] > score
                             select new { Name = student.FirstName, Score = student.ExamScores[exam] };
            sb.AppendFormat("{0,-15}{1}\n", "Name", "Score");
            foreach (var item in highScores)
            {
                sb.AppendFormat("{0,-15}{1}\n", item.Name, item.Score);
            }
            sb.Append("</pre>");
            return sb.ToString();
        }
    }
</script>

<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
    <SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,webpagecreation_bp_title%>" EncodeMethod='HtmlEncode'/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
        <% = new StudentClass().QueryHighScores(1, 90) %><WebPartPages:WebPartZone runat="server" ID="__WebPartPageLibraryZone__"><ZoneTemplate>
<WebPartPages:ContentEditorWebPart runat="server" __MarkupType="xmlmarkup" WebPart="true" __WebPartId="{658D1BBF-E074-44F3-98EC-A8363F60F4AD}" >
<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>Content</Title>
  <FrameType>None</FrameType>
  <Description>Use for formatted text, tables, and images.</Description>
  <IsIncluded>false</IsIncluded>
  <PartOrder>1</PartOrder>
  <FrameState>Normal</FrameState>
  <Height />
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>false</AllowMinimize>
  <AllowConnect>true</AllowConnect>
  <AllowEdit>true</AllowEdit>
  <AllowHide>true</AllowHide>
  <IsVisible>true</IsVisible>
  <DetailLink />
  <HelpLink />
  <HelpMode>Modeless</HelpMode>
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <PartImageLarge />
  <IsIncludedFilter />
  <ExportControlledProperties>true</ExportControlledProperties>
  <ConnectionID>00000000-0000-0000-0000-000000000000</ConnectionID>
  <ID>g_658d1bbf_e074_44f3_98ec_a8363f60f4ad</ID>
  <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
          <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[]]></Content>
  <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>
</WebPartPages:ContentEditorWebPart>
</ZoneTemplate></WebPartPages:WebPartZone></asp:Content>
  
  总结,全部工作都在 web.config 中完成,有两个地方需要配置

  一是,配置 system.codedom/compilers/complier,指示编译器版本
  二是,配置 system.web/compilation/assemblies,添加额外的.NET3.5程序集引用

  以上两个配置节只需要从已存在的 ASP.NET 3.5 应用程序的 web.config 拷贝过去。
  对于 sharepoint 的 webconfig 包含很多其特有属性,直接编辑是一件让人敬畏的事,很容易出错,一不小心就翘辫子了。

  update 2009年1月15日 1:28:37
  
  明天再介绍一种&#8220;自动化&#8221;的方法以及支持ASP.NET AJAX 的测试。
  如何自动化升级 SharePoint 的 web.config 到 .NET 3.5 ? 这里充分利用在 Vistual Studio 2008 中切换 .NET 版本的时候自动化更新 web.config 的特性:

  1. 新建一个 WebApplication 或者 WebSite,Target Framework 选择 .NET framework 2.0
2. 拷贝欲升级的 SharePoint web.config 替换新建站点的 web.config
3. 打开项目属性,选择 Application 选项卡
4. 在 Application 选项卡中,Target Framework 选择 .NET framework 3.5
5. OK,VS 自动帮你更新 web.config 中 ASP.NET 3.5 相关配置节,包括 ASP.NET AJAX
6. 将升级的 web.config 覆盖旧的 SharePoint 站点下的 web.config (注意一定要养成习惯先备份,不能保证更新的web.config完全兼容)

  
  
  
  update 2009年2月5日11:22:39
  在 Windows SharePoint Services 3.0 版中安装 ASP.NET 2.0 AJAX Extensions 1.0

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118443-1-1.html 上篇帖子: SharePoint 2010的网站主题 下篇帖子: SharePoint 2013 Step by Step—— 为终端用户提供故障恢复的解决方案 Part I
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表