Share point 如何更改日历默认显示样式,月,周,日 切换


    • I don't know about you but I find it sometimes rather upsetting when I'm looking to solve a dev task, pick up all possible resources on the subject, only to find out that some of the so much acclaimed resources don't really offer any solution. This, unfortunately, is true nearly all the time when it comes to developing SharePoint solutions and referring to Microsoft's MSDN library or books.

      Ok, let's cut through the chase, I was trying to update the default view style of a list's calendar view created via OM. I created the view using the SPView class and setting it's type property to Calendar. Then I got a collection of the SPCalendarViewStyle objects of the calendar view using the SPCalendarViewStyleCollection. Now all I needed to do was to find the right viewStyle I'd like to set as default and simply set it's properties; you would thought that would work. well I'm afraid that didn't turn out as expected!

      Below is the piece of code I'd written for this (note: this piece of code does NOT solve the problem!):


      private static void SetCalendarViewStyle(string siteUrl,
      string listName, string viewName, string newViewStyle)
      {
      using (SPSite site = new SPSite(siteUrl)){
      using (SPWeb web = site.OpenWeb("/")){
      try{
      SPList list = web.Lists[listName];
      if (list != null){
      SPView view = list.Views[viewName];
      if (view != null){
      bool styleChanged = false;
      SPCalendarViewStyleCollection coll = new SPCalendarViewStyleCollection(web, view);
      Console.WriteLine("Before View Style: {0}", coll.DefaultViewStyle.Type);
      SPSecurity.RunWithElevatedPrivileges(delegate(){
      foreach (SPCalendarViewStyle viewStyle in coll){
      if (viewStyle.Type.ToLower() == newViewStyle.ToLower()){
      // set the new default view style
      viewStyle.Default = true;
      styleChanged = true;
      }
      else{
      // set the default property of all other view style to false
      viewStyle.Default = false;
      }
      }
      if (styleChanged){
      view.Update();
      list.Update();
      Console.WriteLine("default view style changed");
      Console.WriteLine("After View Style: {0}", coll.DefaultViewStyle.Type);
      }
      });
      }
      }
      }
      catch (Exception ex) { throw ex; }
      }
      }
      }

      The list and it's calendar view referred to here were both created using OM and the list isn't listed in the quick launch. I've read a few articles and comments that suggested appending text to the end of the Url property of the view object appeared to address the problem; unfortunately that was not the case for me. Besides, the Url property of the view object is read only and I'd rather not append any text to the value of the Url property.

      To address the issue, I had to use reflection, this gave me a read/write access to the supposedly protected Node property. Then I made changes to the value of the property and simply updated the view object and its associated SPLimitedWebPartManager object.

      This piece of code solve the problem and the approach can be applied to existing calendar views.


      private static void SetCalendarViewStyle(SPWeb web,
      SPView view, DefaultCalendarView defaultView){
      SPLimitedWebPartManager vm =
      web.GetFile(view.Url).GetLimitedWebPartManager(PersonalizationScope.Shared);
      ListViewWebPart lv = null;
      foreach (System.Web.UI.WebControls.WebParts.WebPart part in vm.WebParts){
      if (part.GetType().ToString().ToLower() == "microsoft.sharepoint.webpartpages.listviewwebpart"){
      lv = part as ListViewWebPart;
      break;
      }
      }
      if (lv != null){
      PropertyInfo ViewProp = lv.GetType().GetProperty("View",
      BindingFlags.NonPublic | BindingFlags.Instance);
      SPView spView = ViewProp.GetValue(lv, null) as SPView;

      PropertyInfo viewProp = spView.GetType().GetProperty(
      "SchemaXml", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
      string root = viewProp.GetValue(spView, null) as string;
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(root);
      XmlNode node = doc.SelectSingleNode("/View/CalendarViewStyles");
      if (node == null){
      XmlNode stylesNodes = doc.CreateElement("CalendarViewStyles");
      stylesNodes.InnerXml = SetViewStyleNode(defaultView);
      doc.DocumentElement.AppendChild(stylesNodes);
      }
      else{
      node.InnerXml = SetViewStyleNode(defaultView);
      }
      PropertyInfo NodeProp = spView.GetType().GetProperty(
      "Node", BindingFlags.NonPublic | BindingFlags.Instance);
      XmlNode newNode = NodeProp.GetValue(spView, null) as XmlNode;
      newNode.InnerXml = doc.DocumentElement.InnerXml;
      spView.Update();
      vm.SaveChanges(lv);
      }
      }

作者没有给出下面这个函数。

public string SetViewStyleNode(string viewStyleTypeAsDefault) {

            System.Text.StringBuilder sbViewStyleXml = new System.Text.StringBuilder(16);

            string[] viewStyleStr ={"<CalendarViewStyle  Title='Day Group' Type='daygroup' Template='CalendarViewdaygroupChrome' Sequence='1' Default='{0}' />",

                                    "<CalendarViewStyle  Title='Week Group' Type='weekgroup' Template='CalendarViewweekgroupChrome' Sequence='2' Default='{0}' />",

                                    "<CalendarViewStyle  Title='Day' Type='day' Template='CalendarViewdayChrome' Sequence='3' Default='FALSE' />",

                                    "<CalendarViewStyle  Title='Week' Type='week' Template='CalendarViewweekChrome' Sequence='4' Default='FALSE' />",

                                    "<CalendarViewStyle  Title='Month' Type='month' Template='CalendarViewmonthChrome' Sequence='5' Default='{0}' />"

                                   };

            switch (viewStyleTypeAsDefault) {

                case "daygroup":

                    viewStyleStr[0] = string.Format(viewStyleStr[0], "TRUE");

                    viewStyleStr[1] = string.Format(viewStyleStr[0], "FALSE");

                    viewStyleStr[4] = string.Format(viewStyleStr[4], "FALSE");

                    break;

                case "weekgroup":

                    viewStyleStr[0] = string.Format(viewStyleStr[0], "FALSE");

                    viewStyleStr[1] = string.Format(viewStyleStr[0], "TRUE");

                    viewStyleStr[4] = string.Format(viewStyleStr[4], "FALSE");

                    break;

                default:

                    viewStyleStr[0] = string.Format(viewStyleStr[0], "FALSE");

                    viewStyleStr[1] = string.Format(viewStyleStr[0], "FALSE");

                    viewStyleStr[4] = string.Format(viewStyleStr[4], "TRUE");

                    break;

            }

            foreach (string strStyle in viewStyleStr) {

                sbViewStyleXml.Append(strStyle);

            }

            string viewStyleXml = sbViewStyleXml.ToString();

            sbViewStyleXml = null;

            return viewStyleXml;

        }

 

===viewStyleTypeAsDefault:

month

daygroup

weekgroup

你可以写上day,week分支。

 

在PowerPages中,下载SharePoint文件通常是通过调用SharePoint Web Services API或者利用其内置的Microsoft Office SharePoint Foundation (MOSS) Web Services来完成的。这里是一个基本的步骤: 1. **设置环境**: 确保你已经安装了SharePoint的SOAP服务客户端库,例如Windows SharePoint Services SDK或SharePoint Server Management Shell工具。 2. **访问SharePoint站点**: 需要使用SharePoint的对象模型(如SPSite、SPList和SPFile)来与SharePoint交互。首先,你需要凭据(如用户名和密码)获取对SharePoint网站的访问权限: ```powerpages Dim spSite As SPSite Dim spWeb As SPWeb Set spSite = New SPSite("http://your-sharepoint-site") Set spWeb = spSite.OpenWeb() ``` 3. **定位文件**: 根据文件路径或列表ID找到你要下载的文件: ```powerpages Dim spList As SPSList Dim spFile As SPFile Set spList = spWeb.Lists("Documents") ' 替换为你的文档列表名 Set spFile = spList.Items.ItemById("file-id") ' 或者 spList.GetFileByServerRelativeUrl("/your-file-path") ``` 4. **下载文件**: 使用`spFile.OpenBinaryStream`方法读取文件内容,然后将其写入HTTP响应以便下载: ```powerpages Dim binaryStream As Stream binaryStream = spFile.OpenBinaryStream() Response.ContentType = "application/octet-stream" Response.AddHeader("Content-Disposition", "attachment; filename=your-file-name") ' 替换为文件的实际名称 Response.BinaryWrite(binaryStream.ReadAll()) binaryStream.Close() ``` 5. **清理资源**: 关闭流和对象以释放内存: ```powerpages binaryStream.Close() Set binaryStream = Nothing Set spFile = Nothing spWeb.Dispose() spSite.Dispose() ``` 注意:以上代码示例是理论上的,实际使用时需要处理异常,以及可能的安全限制,比如权限控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值