使用RPC(Vbscript)将文件上载到SharePoint

时间:2011-10-19 17:43:30

标签: sharepoint vbscript rpc

我目前正在使用以下VB脚本和首页RPC调用将文档及其透视元数据值(在脚本中配置)上载到Sharepoint 2007和2010站点。但是,扩展名为* .xlsx或* .docx的文件无法使用此脚本上载其元数据。

在研究各种网站之后,_vti_aut / author.dll处理WSS 3.0中新的开放式办公格式的方式似乎存在问题。一些建议是在将文件上载到SharePoint站点后使用SetDocsMetaInfo方法。

问题是,我不明白如何在此脚本中使用此方法。当我尝试使用它时,我能够使用正确的元数据上传* .xlsx或* .docx文件,但随后文件被破坏。

我研究过的一些网站已经建议它可能是文件编码的方式,但我不熟悉编码以准确排除故障。

有人可以通过以下脚本提供有关如何使用此方法(SetDocsMetaInfo)的示例,或者使用此脚本提供此问题的解决方案吗?

这个原始脚本是从这个网站上检索到的:

UploadFile "C:\Test\Work\temp\defer\testDoc_083011.xlsx", _     
"http://sharepoint.domainname.com/Sites/SITE1", _     
"TestImport/folder1/testDoc_083011.xlsx_083011.xlsx", _     
"Test", _     
"Test checkin comment", _     
"", "" 

MsgBox "Done" 

Function StringToByteArray(str)    

    Set stream = CreateObject("ADODB.Stream")    
    stream.Open    
    stream.Type = 2 ''adTypeText    
    stream.Charset = "ascii"    
    stream.WriteText str    
    stream.Position = 0    
    stream.Type = 1 ''adTypeBinary    
    StringToByteArray = stream.Read()    
    stream.Close 

End Function  

Sub UploadFile(sourcePath, siteUrl, docName, title, checkincomment, userName, password)  

    strHeader = "method=put+document%3a12.0.4518.1016" + _       
    "&service_name=%2f" + _       
    "&document=[document_name=" + Escape(docName) + _       
    ";meta_info=[vti_title%3bSW%7c" + Escape(title) + ";Business Unit%3bSW%7c" + Escape("Business Unit")+ "]]" + _       
    "&put_option=overwrite,createdir,migrationsemantics" + _       
    "&comment=" + _       
    "&keep%5fchecked%5fout=false" + vbLf 

    bytearray = StringToByteArray(strHeader)    

    Set stream = CreateObject("ADODB.Stream")    
    stream.Open    
    stream.Type = 1 ''adTypeBinary    
    stream.Write byteArray 

    Set stream2 = CreateObject("ADODB.Stream")    
    stream2.Open    
    stream2.Type = 1 ''adTypeBinary    
    stream2.LoadFromFile sourcePath    
    stream2.CopyTo stream, -1    
    stream.Position = 0     

    Set xmlHttp = CreateObject("MSXML2.XMLHTTP")    
    xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password    
    xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"    
    xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"    
    xmlhttp.setRequestHeader "User-Agent", "FrontPage"    
    xmlHttp.send stream     

    If xmlHttp.status = 200 Then         
        If Instr(xmlHttp.responseText, "successfully") = 0 Then            
            MsgBox "ERROR: " & vbCrLf & xmlHttp.responseText                
        Else            

            ''Checkin            
            strHeader = "method=checkin+document%3a12.0.4518.1016" + _              
            "&service_name=%2f" + _              
            "&document_name=" & Escape(docName) + _              
            "&comment=" + Escape(checkincomment) + _              
            "&keep%5fchecked%5fout=false" + vbLf      

            Set xmlHttp = CreateObject("MSXML2.XMLHTTP")           
            xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password           
            xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"           
            xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"           
            xmlhttp.setRequestHeader "User-Agent", "FrontPage"           
            xmlHttp.send strHeader           
        End If     
    End If     

      If xmlHttp.status / 100 <> 2 Then       
        MsgBox "ERROR: status = " & xmlHttp.status & vbCrLf & xmlHttp.responseText    
      End If  
End Sub  

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,并发现使用一个明显已弃用的例程,按照下面的代码设置文档元信息工作正常:

Public Sub SetSPMetaData(ByVal sURL As String, ByVal sDocName As String, ByVal sTitle As    String, _
                     Optional ByVal OriginalPath As String, Optional ByVal OriginalName As String, _
                     Optional ByVal ModDate As Date, Optional ByVal FileID As Long)

Dim strHeader As String
Dim byteArray() As Byte
Dim stream As New ADODB.stream
Dim stream2 As New ADODB.stream
Dim xmlHTTP As New MSXML2.xmlHTTP
Dim sTempFile As String
Dim UserName As String
Dim Password As String


On Error GoTo SetSPMetaData_Error

'Method = setDocsMetaInfo: server_extension_version
'&service_name=/[&listHiddenDocs=(true|false)]
'&listLinkInfo=(true|false)&url_list=list_of_urls
'&metaInfoList=(list_of_meta_info)[&errorFlags=(KeepGoing|StopOnFirst)]


'POST /site_url/_vti_bin/_vti_aut/author.dll HTTP/1.0
'.
'.
'.
'method=set+docs+meta+info:6.0.n.nnnn
'&service_name=/
'&url_list=[List_Name/File_Name]
'&metaInfoList=[[vti_title;SR|Web+Settingt;SW|fp40]]true
'&listLinkInfo=true

If OriginalPath = "" And OriginalName = "" Then
    strHeader = "method=set+document+meta-info:6.0.n.nnnn" + _
                "&service_name=/&document_name=" & sDocName & _
                "&meta_info=[vti_title;SR|" & Replace(Escape(sTitle), "%5C", "%5C%5C") & "]"
Else

    If OriginalName = "" Then
        If CLng(ModDate) = 0 Then

            strHeader = "method=set+document+meta-info:6.0.n.nnnn" + _
                        "&service_name=/&document_name=" & sDocName & _
                        "&meta_info=[vti_title;SR|" & Replace(Escape(sTitle), "%5C", "%5C%5C") & _
                        ";Original Path;SR|" & Replace(Escape(OriginalPath), "%5C", "%5C%5C") & "]"


        Else

            strHeader = "method=set+document+meta-info:6.0.n.nnnn" + _
                        "&service_name=/&document_name=" & sDocName & _
                        "&meta_info=[vti_title;SR|" & Replace(Escape(sTitle), "%5C", "%5C%5C") & _
                        ";Original Modified|" & Format(ModDate, "DD MMM YYYY hh:mm:ss") & _
                        ";Original Path;SR|" & Replace(Escape(OriginalPath), "%5C", "%5C%5C") & "]"

        End If

    Else

        If CLng(ModDate) = 0 Then
            strHeader = "method=set+document+meta-info:6.0.n.nnnn" + _
                        "&service_name=/&document_name=" & sDocName & _
                        "&meta_info=[vti_title;SR|" & Replace(Escape(sTitle), "%5C", "%5C%5C") & _
                        ";Original Name;SR|" & Replace(Escape(OriginalName), "%5C", "%5C%5C") & _
                        ";Original Path;SR|" & Replace(Escape(OriginalPath), "%5C", "%5C%5C") & "]"
        Else

            strHeader = "method=set+document+meta-info:6.0.n.nnnn" + _
                        "&service_name=/&document_name=" & sDocName & _
                        "&meta_info=[vti_title;SR|" & Replace(Escape(sTitle), "%5C", "%5C%5C") & _
                        ";Original Name;SR|" & Replace(Escape(OriginalName), "%5C", "%5C%5C") & _
                        ";Original Modified;TW|" & Format(ModDate, "DD MMM YYYY hh:mm:ss") & _
                        ";Original Path;SR|" & Replace(Escape(OriginalPath), "%5C", "%5C%5C") & "]"

        End If
    End If
End If

byteArray = StringToByteArray(strHeader)

'Set stream = CreateObject("ADODB.Stream")

If gConfig.GetConfig("frmHTTPAuthentication") = 2 Then
    If gConfig.GetConfig("txtUserPassword") <> "" Then Password = gConfig.GetConfigDecrypt("txtUserPassword")
    If gConfig.GetConfig("txtUserName") <> "" Then UserName = gConfig.GetConfigDecrypt("txtUserName")
End If

stream.Open
stream.Type = 1    ''adTypeBinary
stream.Write byteArray


stream.Position = 0
'stream.SaveToFile "C:\StreamContent.txt"

xmlHTTP.Open "POST", sURL + "/_vti_bin/_vti_aut/author.dll", False, UserName, Password
xmlHTTP.setRequestHeader "Content-Type", "application/x-vermeer-urlencoded"
xmlHTTP.setRequestHeader "X-Vermeer-Content-Type", "application/x-vermeer-urlencoded"
xmlHTTP.setRequestHeader "User-Agent", "FrontPage"
xmlHTTP.send stream

If xmlHTTP.Status = 200 Then

    If InStr(xmlHTTP.responseText, "osstatus=0") <> 0 Then
        '230               MsgBox "Error - " & cleanup_html(xmlHTTP.responseText)
        Call LogErrorFilFol(GetFileFromPath(sURL), GetFolderFromPath(sURL), _
                            "SetSPMetaData - osstatus = 0 - " & cleanup_html(xmlHTTP.responseText), FileID)
    End If
    '260           Debug.Print xmlHTTP.responseText

Else

    Call LogErrorFilFol(GetFileFromPath(sURL), GetFolderFromPath(sURL), "SetSPMetaData - Status = " & xmlHTTP.Status, FileID)

End If