2014-02-27 59 views
3

我准备使用Timeglider创建时间轴。一个要求是数据必须采用JSON格式。对我的一个要求是它需要成为客户端,因为我无法访问服务器或中央管理员。以JSON格式查看SharePoint 2010列表

当我尝试做http://webname/_vti_bin/ListData.svc/listname但是当我发出访问权限时出现错误http://webname/subsite/_vti_bin/ListData.svc/listname我没有问题拉取数据。

我的情况是列表在TLD上。我试图关注此帖How to retrieve a json object from a sharepoint list,但它与SP 2007有关。

+0

您是否考虑过使用sp.js公开csom api over javascript? –

+0

我该怎么做。我不是一个SharePoint专家。 – JeremyA1

+0

但你确实知道JavaScript,在这种情况下你的技能水平是什么? –

回答

1

要在SharePoint 2007,2010等平台上实现纯JSON支持,请查看此项目http://camelotjson.codeplex.com/。它要求将商业产品Camelot .NET Connector安装在服务器上。

如果你不喜欢去商业你可以诉诸sp.js图书馆,这里是我写的一个小例子,享受!

// Object to handle some list magic 
var ListMagic = function() { 
    /* Private variables */ 
    var that = this; 
    var clientContext = SP.ClientContext.get_current(); 
    var web = clientContext.get_web(); 
    var lists = web.get_lists(); 

    /** 
    * Method to iterate all lists 
    */ 
    that.getLists = function() { 

     clientContext.load(lists); 
     clientContext.executeQueryAsync(execute, getFailed); 

     function execute() { 
      var listEnumerator = lists.getEnumerator(); 
      while (listEnumerator.moveNext()) { 
       var l = listEnumerator.get_current(); 
       // TODO! Replace console.log with actual routine 
       console.log(l.get_title()); 
      } 
     } 

     function getFailed() { 
      // TODO! Implement fail management 
      console.log('Failed.'); 
     } 
    }; 


    /** 
    * Method to iterate all fields of a list 
    */ 
    that.getFields = function (listName) { 

     // Load list by listName, if not stated try to load the current list 
     var loadedList = typeof listName === 'undefined' ? lists.getById(SP.ListOperation.Selection.getSelectedList()) : that.lists.getByTitle(listName); 
     var fieldCollection = loadedList.get_fields(); 

     clientContext.load(fieldCollection); 
     clientContext.executeQueryAsync(execute, getFailed); 

     function execute() { 
      var fields = fieldCollection.getEnumerator(); 
      while (fields.moveNext()) { 
       var oField = fields.get_current(); 

       // TODO! Replace console.log with actual routine 
       var listInfo = 'Field Title: ' + oField.get_title() + ', Field Name: ' + oField.get_internalName(); 
       console.log(listInfo); 
      } 
     } 

     function getFailed() { 
      // TODO! Implement fail management 
      console.log('Failed.'); 
     } 
    }; 


    /** 
    * Method to get a specific listitem 
    */ 
    that.getListItem = function (itemId) { 

     var loadedList = lists.getById(SP.ListOperation.Selection.getSelectedList()); 
     var spListItem = loadedList.getItemById(itemId); 

     clientContext.load(spListItem); 
     clientContext.executeQueryAsync(execute, getFailed); 


     function execute() { 
      // TODO! Replace console.log with actual routine 
      //spListItem.get_fieldValues() 
      console.log(spListItem.get_fieldValues()["Title"]); 
     } 

     function getFailed() { 
      // TODO! Implement fail management 
      console.log('Failed.'); 
     } 
    }; 

    /** 
    * Method to fake an init (optional) 
    */ 
    that.init = function() { 
     // Run any init functionality here 
     // I.e 
     that.getFields("Tasks"); 
    }; 

    return that; 
}; 

// In case of no jquery use window.onload instead 
$(document).ready(function() { 
    ExecuteOrDelayUntilScriptLoaded(function() { 
     var sp = new ListMagic(); 
     sp.init(); 
    }, 'sp.js'); 
}); 
+0

我将在我重新开始工作时尝试此操作。在我离开之前,我使用了这个exmaple [链接](http://mydevexperience.wordpress.com/2011/05/11/sharepoint-list-access-part-iii-how-to-access-sharepoint-windows-communication-foundation -wcf-rest-representational-state-transfer-services-using-jquery-completely-client-side-only /) – JeremyA1

+0

我可以在窗口中看到输出。然而,Timeglider需要一个文件或URL来通过JSON格式加载数据。我的问题是我所指的列表是日历列表。我的目标是这个SharePoint列表 - >输出到XML - >转换为JSON - >加载到时间轴。 – JeremyA1

+0

卡米洛特JSON是您的最佳选择 –

0

就我个人而言,我制作HttpHandlers。我将它们安装在SharePoint isapi文件夹和GAC中,我可以像调用owssvr.dll一样调用它们。 http://servername/_vti_bin/myhttphandelr.dll 传递查询字符串变量或从jquery ajax调用它。您可以使用httpcontext并从中创建spcontext,并可以访问SharePoint中当前位置的各种信息。然后你可以javascriptserialize这些对象并将它们作为JSON传递。寻找一些代码...挂上...我不能把所有的代码,但这应该让你关闭。我用它来添加一个子菜单到上下文菜单,以允许用户删除或重命名一个文件,如果他们上传到一个库,它是版本1.0,并从库中收集文件,并创建一个带有选定文件的eml文件(s)作为附件。我们不会给我们的用户正常删除权限。需要指出的是,现在您可以使用SharePoint所需的信息创建一个类,并将其作为JSON传递。这是我唯一的缺点,如果你对dll进行任何更改,iisreset是必需的。 无论如何,我每天晚上都会安排一次iisreset,以保持新鲜感并避免内存膨胀。我第二天来,我的变化就在那里。最酷的是,spcontext具有关于调用SharePoint的当前位置的信息。所以,http://servername/_vti_bin/myhttphandelr.dll vs http://servername/subsite/library/_vti_bin/myhttphandelr.dll

我可能会添加。不要尝试序列化SharePoint对象。一个是巨大而复杂的物体。二,我不认为它们被标记为可序列化。只需创建自己的类,并使用SharePoint对象中需要的值填充它。

using System; 
using System.Collections.Generic; 
using System.Runtime.InteropServices.ComTypes; 
using System.Web; 
using System.Web.Script.Serialization; 
using ADODB; 
using interop.cdosys; 
using Microsoft.SharePoint; 


namespace owssvr2 
{ 
    public class OWSsvr2 : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    { 

     private string cmd; 
     ctx ctx = new ctx(); 
     private string currentuser; 
     private SPContext SPcontext; 
     private HttpContext cntx; 
     public bool IsReusable 
     { 
      get { return false; } 
     } 

     public void ProcessRequest(HttpContext context) 
     { 
      SPcontext = SPContext.GetContext(context); <-- Gets spcontext from the httpcontext 
      cntx = context; 
      ctx = GetData(context.Request); <-- I parse some information from the request to use in my app 
      cmd = ctx.Cmd; 
      ctx.User = context.User.Identity.Name; 
      currentuser = context.User.Identity.Name; 
      switch (cmd) 
      { 
       case "Delete": 
        Delete(); 
        context.Response.Redirect(ctx.NextUsing); 
        break; 
       case "HasRights": 
        HasRights(); 

         JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); 
      string serEmployee = javaScriptSerializer.Serialize(ctx); 
      context.Response.Write(serEmployee); 
      context.Response.ContentType = "application/json; charset=utf-8"; 
        break; 
       case "Rename": 
        Rename(context); 
        //context.Response.Redirect(context.Request["NextUsing"]); 
        break; 
       case "SendSingleFile": 
        try 
        { 
         context.Response.Clear(); 
         context.Response.ClearHeaders(); 
         context.Response.BufferOutput = true; 
         ADODB.Stream stream = SendSingleFile(context.Request["URL"]); 
         stream.Type = StreamTypeEnum.adTypeBinary; 
         stream.Position = 0; 
         context.Response.ContentType = "application/octet-stream"; 
         context.Response.AddHeader("content-disposition", "attachment;filename=Email.eml"); 
         IStream iStream = (IStream)stream; 
         byte[] byteArray = new byte[stream.Size]; 
         IntPtr ptrCharsRead = IntPtr.Zero; 
         iStream.Read(byteArray, stream.Size, ptrCharsRead); 
         context.Response.BinaryWrite(byteArray); 
         context.Response.End(); 
        } 
        catch(Exception ex) {context.Response.Write(ex.Message.ToString()); } 
        break; 

       case "SendMultiFile": 

        try 
        { 
         //SendMultiFile(context.Request["IDs"]); 
         context.Response.Clear(); 
         context.Response.ClearHeaders(); 
         context.Response.BufferOutput = true; 
         ADODB.Stream stream = SendMultiFile(context.Request["IDs"]); 
         stream.Type = StreamTypeEnum.adTypeBinary; 
         stream.Position = 0; 
         context.Response.ContentType = "application/octet-stream"; 
         context.Response.AddHeader("content-disposition", "attachment;filename=Email.eml"); 
         IStream iStream = (IStream)stream; 
         byte[] byteArray = new byte[stream.Size]; 
         IntPtr ptrCharsRead = IntPtr.Zero; 
         iStream.Read(byteArray, stream.Size, ptrCharsRead); 
         context.Response.BinaryWrite(byteArray); 
         context.Response.End(); 
        } 
        catch(Exception ex) {context.Response.Write("There was an error getting the files. </br>" + ex.Message.ToString()); } 
        break; 
       case "FileInfo": 
        JavaScriptSerializer javaScriptSerializer1 = new JavaScriptSerializer(); 
        string serEmployee1 = javaScriptSerializer1.Serialize(FileInfo(context)); 
        context.Response.Write(serEmployee1); 
      context.Response.ContentType = "application/json; charset=utf-8"; 

        break; 
       case "UsersInGroups": 
        UsersInGroups ug = new UsersInGroups(context, context.Request["job"],context.Request["groups"]); 
        break; 
      } 


     }