如何在Sharepoint中更新文档的元数据? (Linux - > WebServices - > Sharepoint)

时间:2010-04-13 13:33:57

标签: python linux sharepoint web-services

我设法将一个文件(crud PUT khe khe :)从Linux上传到Sharepoint。 文件的绝对路径是:

http://myhost/mysite/reports/2010-04-13/file.txt

现在,我正在尝试向文件中添加一些元数据:

from suds.transport.https import WindowsHttpAuthenticated
url='http://myhost/mysite/_vti_bin/lists.asmx?WSDL'
n=WindowsHttpAuthenticated(username='me',password='password')
from suds.client import Client
c=Client(url,transport=n)

xml="""<Batch OnError='Continue' PreCalc='' ListVersion='0'>
<Method ID='1' Cmd='Update'>
    <Field Name='ID'/>
    <Field Name='FileRef'>%s</Field>
    <Field Name='Jurisdiction'>%s</Field>
</Method>
</Batch>"""
fn = 'http://myhost/mysite/reports/2010-04-13/file.txt'
print c.service.UpdateListItems('reports',xml % (fn,'UK'))

代码返回:

soap:Server

...而且没有发生。 我错过了什么吗?还有其他办法吗?

由于

2 个答案:

答案 0 :(得分:4)

发现它! :)

而不是纯文本 XML 必须使用 DOM 对象,如下所示:

b = Element("Batch")
b.append(Attribute("OnError","Continue")).append(Attribute("ListVersion","3"))
bm= Element("Method")
bm.append(Attribute("ID","1")).append(Attribute("Cmd","Update"))
bm.append(Element("Field").append(Attribute("Name","ID")).setText(''))
bm.append(Element('Field').append(Attribute('Name','FileRef')).setText('http://.....'))
bm.append(Element('Field').append(Attribute('Name','Jurisdiction')).setText('UK'))
bm.append(Element('Field').append(Attribute('Name','Desk')).setText('Structured Equity Derivatives'))
bm.append(Element('Field').append(Attribute('Name','Business Area')).setText('Back Office'))
bm.append(Element('Field').append(Attribute('Name','Title')).setText('whatever'))
b.append(bm)
u = Element("ns1:updates")
u.append(b)
c.service.UpdateListItems("Reports",u)

现在它完美无缺!

答案 1 :(得分:3)

根据要求,创建新文件夹并从linux命令行将文件上载到SharePoint站点的示例脚本。 完整的SharePoint路径如下所示:

http:// mysite / MyFirstSPSite / Reports / [current_iso_date] / [uploaded_file.txt]

#!/usr/bin/python2.4

import datetime as dt
import sys
from suds.transport.https import WindowsHttpAuthenticated
from suds.sax.element import Element
from suds.sax.element import Attribute
from suds import client
from ntlm import HTTPNtlmAuthHandler
import urllib2
import os.path

FOLDER = dt.date.today().strftime("%Y-%m-%d")  #folder name that will be created
FNAME = sys.argv[1]                            #file name to upload
SITE = "http://mysite/MyFirstSPSite"
FURL = "%s/Reports/%s/%s" % (SITE,FOLDER,os.path.basename(FNAME))
USER = "uk\\user_name_goes_here"   # AD user name
PASS = "password_goes_here"

def main():
  wss_lists = client.Client("%s/_vti_bin/lists.asmx?WSDL" %      SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
  wss_dws = client.Client("%s/_vti_bin/dws.asmx?WSDL" % SITE,transport=WindowsHttpAuthenticated(username=USER,password=PASS))
  wss_dws.service.CreateFolder("Reports/%s" % FOLDER)
  print uploadReport(FURL,sys.argv[1])
  wss_lists.service.UpdateListItems("Reports",getUpdatesElement(FURL,"Title goes here"))


def getUpdatesElement(furl,title = ''):
  b = Element("Batch")
  b.append(Attribute("OnError","Continue")).append(Attribute("ListVersion","3"))
  bm= Element("Method")
  bm.append(Attribute("ID","1")).append(Attribute("Cmd","Update"))
  bm.append(Element("Field").append(Attribute("Name","ID")).setText(''))
  bm.append(Element('Field').append(Attribute('Name','FileRef')).setText(furl))
  bm.append(Element('Field').append(Attribute('Name','CustomProperty1')).setText('Value1'))
  bm.append(Element('Field').append(Attribute('Name','CustomProperty2')).setText('Value2'))
  bm.append(Element('Field').append(Attribute('Name','Title')).setText(title))
  b.append(bm)
  u = Element("ns1:updates")
  u.append(b)
  return u


def uploadReport(furl,fname):
  pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
  pm.add_password(None,'http://mysite',USER,PASS)
  op = urllib2.build_opener(HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pm))
  #import pdb;pdb.set_trace()
  fh = open(fname)
  data = fh.read()
  fh.close()
  req = urllib2.Request(furl,data=data)
  req.get_method = lambda: 'PUT'
  req.add_header('Content-Type','text/csv')
  r = op.open(req)
  return r.read()

if __name__=="__main__": main()

希望有帮助:)