从MVC站点查询共享点列表

时间:2014-02-02 20:45:45

标签: asp.net-mvc sharepoint

好的,我有一个相当简单的请求,但我现在已经搜索了几个小时,似乎无法找到一个简单的答案,我觉得应该是一个相当简单的任务。

我使用dot net& amp;为我们的业务建立了一个内部网站点。 MVC在单独的服务器上运行到sharepoint站点。我想要做的就是从给定的sharepoint库中获取文档列表,并显示指向其URL的链接,这些链接将最终用户从我的Intranet站点重定向到该文档 - 这将根据用户部门的不同而不同。

所以我正在查看jquery AJAX路由,但后来我意识到sharepoint不会允许我来自不同域的javascript查询。

所以下一个路由是后端dot net将数据传递给视图并以这种方式显示,但我想使用服务器sharepoint身份验证,而无需在代码中手动输入用户名和密码。

我错过了一些简单的东西吗?我不想浪费很多时间在我认为应该相当简单的事情上......有人想把我指向正确的方向吗?

由于

1 个答案:

答案 0 :(得分:1)

嗯,这并不容易,但如果有人想知道,我确实这样做了:p sp 2013使用Rest api使用服务器凭据连接到sharepoint并检索文档Name,Sharepoint url和icon。

Imports System.Xml
Imports System.IO
Imports System.DirectoryServices.AccountManagement
Imports System.Linq
Imports System.Net

Public Class SPDocumentLibrary

  Public Property Files As List(Of SPDocument)

  Public Sub New()

    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(
    "http://sharepointserver/_api/web/lists/getByTitle('list')/items?$expand=File&$select=File/Name,OData__dlc_DocIdUrl/Url&$orderby=Title&$top=99999&orderby=File/Name"
                          ), HttpWebRequest)

    request.Method = "GET"
    request.ContentType = "text/xml"
    request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
    Dim receiveStream As Stream = response.GetResponseStream()
    Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
    Dim xd As XDocument = XDocument.Load(readStream)
    response.Close()
    readStream.Close()

    Dim d As XNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices"
    Dim x As XNamespace = "http://www.w3.org/2005/Atom"

    Dim docs As IEnumerable(Of SPDocument) = _
    From doc As XElement In xd.Root.Elements(x + "entry") _
    Select New SPDocument With {.Name = Path.GetFileNameWithoutExtension(doc.Descendants(d + "Name").Value), .Url = doc.Descendants(d + "Url").Value, .icon = IconFromExtension(Path.GetExtension(doc.Descendants(d + "Name").Value).ToLower)}

    Me.Files = docs.ToList()

End Sub

Private Function IconFromExtension(ext As String) As String

    If ext Like ".doc*" Then
        Return "icdoc.png"
    ElseIf ext Like ".xls*" Then
        Return "icxls.png"
    ElseIf ext Like ".ppt*" Then
        Return "icppt.png"
    ElseIf ext = ".pdf" Then
        Return "icpdf.png"
    ElseIf ext = ".msg" Then
        Return "icmsg.png"
    ElseIf ext Like ".htm*" Then
        Return "ichtm.gif"
    ElseIf ext Like ".rtf" Then
        Return "icrtf.gif"
    ElseIf ext Like ".dot*" Then
        Return "icdot.png"
    ElseIf ext Like ".xlt*" Then
        Return "icxlt.png"
    Else
        Return "icgen.gif"
    End If

  End Function

End Class

Public Class SPDocument
  Property Name As String
  Property Url As String
  Property icon As String
End Class