こんにちは、
私は、ライブラリ内のいくつかのアイテムを見つけてダウンロードできるようにする再帰ループに取り組んできました。
これまでのところ、すべての要素を取得できましたが、要素のプロパティに「名前」フィールドが表示されていないため、ダウンロードできません。
要素をファイルに変換すると、プロパティがnull({})になるため、名前を取得できません。
file.dowload()methotはファイルを必要とし、ファイルの現在の拡張子がわからないため、ファイルをダウンロードすることはかなり不可能です。
何か疑惑はありますか?
私が使用しているコードは次のとおりです。
from office365.runtime.auth.ClientCredential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
from office365.sharepoint.camlQuery import CamlQuery
ctx = ClientContext.connect_with_credentials(site_url, ClientCredential(client_id, client_secret))
lib = ctx.web.lists.get_by_title("XXX")
caml_query = CamlQuery()
caml_query.ViewXml = """XXX"""
items = lib.get_items(caml_query)
ctx.load(lib)
ctx.execute_query()
for item in items:
file = item.file
with open(download_path, "wb") as local_file:
file.download(local_file)
ctx.execute_query()
print("[Ok] file has been downloaded: {0}".format(download_path))
前もって感謝します!
おはよう、
私は自分の問題を解決することができました。
アイテムリスト内を繰り返した後、各ファイルのコンテキストを呼び出す必要があります。名前を含むファイルのプロパティが取得され、ファイルをダウンロードできます。
これは以下の私のコードです:
client_id = "XXX"
client_secret = "XXX"
site_url = "XXX"
download_folder = r'C:\Users\yourname\sharepoint'
from office365.runtime.auth.ClientCredential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
from office365.sharepoint.camlQuery import CamlQuery
ctx = ClientContext.connect_with_credentials(site_url, ClientCredential(client_id, client_secret))
lib = ctx.web.lists.get_by_title("REO")
caml_query = CamlQuery()
caml_query.ViewXml = XXX
items = lib.get_items(caml_query)
ctx.load(items)
ctx.execute_query()
for item in items:
file = item.file
ctx.load(file)
ctx.execute_query()
file_name = "{0}".format(file.properties["Name"])
download_path = download_folder + '//' + file_name
with open(download_path, "wb") as local_file:
file.download(local_file)
ctx.execute_query()
print("[Ok] file has been downloaded: {0}".format(download_path))
@ pobs93ソースコードを使用してSharepointリストから添付ファイルをダウンロードしようとしています。
使用するパラメーターがわからないため、この行にコメントしました。
caml_query.ViewXml = XXX
しかし、コードを実行すると、 ctx.execute_query()
行に以下のエラーが表示されます。
TypeError:CamlQueryタイプのオブジェクトはJSONシリアル化できません
私はこのタイプのAPIを初めて使用するので、明確にできるかどうか疑問に思いました。
TKS
@ pobs93添付ファイルをダウンロードしようとしているリストについての説明を追加するだけです。
これらは私が持っているリストのDEFAULTキーであり、ここには添付ファイルのURLは表示されませんが、そこにあります(添付の画像を参照)。
{'FileSystemObjectType':-、
'Id':-、
'ServerRedirectedEmbedUri':なし、
'ServerRedirectedEmbedUrl': ''、
'ContentTypeId':-、
'題名': -、
'ComplianceAssetId':なし、
'ID':-、
「変更済み」:-、
'作成した': -、
'AuthorId':12、
'EditorId':12、
'OData__UIVersionString': '1.0'、
「添付ファイル」:True、
'GUID':-}
次の例は、リストアイテムの_filename_と_fileurl_を取得する方法を示しています。
ctx = ClientContext(settings['url']).with_credentials(client_creds)
lib = ctx.web.lists.get_by_title("Documents")
items = lib.items
ctx.load(items, ["ID", "FileLeafRef", "FileRef"])
ctx.execute_query()
for item in items:
print(f"FileRef: {item.properties['FileRef']}")
どこ
FileLeafRef
-ListItemに関連付けられているファイルノードのサーバー相対URLに関する情報を含むフィールドを識別します
FileRef
-ListItemのサーバー相対URLに関する情報を含むフィールドを識別します
最も参考になるコメント
おはよう、
私は自分の問題を解決することができました。
アイテムリスト内を繰り返した後、各ファイルのコンテキストを呼び出す必要があります。名前を含むファイルのプロパティが取得され、ファイルをダウンロードできます。
これは以下の私のコードです: