Загрузка файла в онлайн-библиотеку Sharepoint

Это можно легко добиться с помощью ванильного JS. См. Ниже.

const data = {
   "directories":{
      "vegetables":{
         "info":{
            "name":"Vegetables"
         },
         "files":{

         },
         "directories":{
            "green vegetables":{
               "info":{
                  "name":"Green Vegetables"
               },
               "files":{

               },
               "directories":{
                  "lettuce":{
                     "info":{
                        "name":"Lettuce"
                     },
                     "files":{

                     }
                  },
                  "cucumber":{
                     "info":{
                        "name":"Cucumber"
                     },
                     "files":{

                     }
                  }
               }
            },
            "orange vegetables":{
               "info":{
                  "name":"Orange Vegetables"
               },
               "files":{

               },
               "directories":{
                  "3 deep":{
                     "info":{
                        "name":"Carrot"
                     },
                     "files":{

                     }
                  }
               }
            }
         }
      },
      "fruit":{
         "info":{
            "name":"Fruit"
         },
         "files":{

         },
         "directories":{
            "apple":{
               "info":{
                  "name":"Apple"
               },
               "files":{

               }
            }
         }
      }
   }
}

function buildList(dataRoot, elementRoot) {

  // Only continue if there are directories to look at
  if (dataRoot.directories) {
  
    // Create a new list and append it to our current element
    const list = document.createElement('ul');
    elementRoot.appendChild(list);
    Object.keys(dataRoot.directories).forEach(key => {
    
      // Get the directory from the key
      const directory = dataRoot.directories[key];
      
      // Create a text node and a list element to put it in
      const listElement = document.createElement('li');
      const textNode = document.createTextNode(directory.info.name);
      listElement.appendChild(textNode);
      list.appendChild(listElement);
      
      // Continue recursively down now using the current lis element
      buildList(directory, listElement);
    });
  }
}

const rootElement = document.getElementById("root");
buildList(data, rootElement);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="root"></div>

0
задан Matthew 18 January 2019 в 22:37
поделиться

1 ответ

Действительно, в случае, если сайт адресуется по URL-адресу сервера , следующий запрос

GET https://graph.microsoft.com/v1.0/sites/{hostname}:/{server-relative-path}:/drive

успешно выполняется и возвращает диск по умолчанию, а следующее :

GET https://graph.microsoft.com/v1.0/sites/{hostname}:/{server-relative-path}:/drive/root

завершается ошибкой и возвращает ошибку Url specified is invalid.

Кажется, это ошибка в самом Microsoft Graph. В любом случае можно использовать следующую опцию для загрузки файла в библиотеку.

Предполагается, что siteUrl соответствует относительному URL сервера сайта, а listId - уникальному идентификатору библиотеки

. Он состоит из следующих шагов:

[ 1126]
  • разрешить сайт по относительному URL сервера
  • получить доступ к корневой папке для библиотеки
  • загрузить файл
    • Пример

      //1.resolve site by server relative url
      var targetSite = await graphClient.Sites.GetByPath(siteUrl,hostName).Request().GetAsync();    //2.access root folder for for a Library
      var targetFolder = graphClient.Sites[targetSite.Id]
             .Lists[listId]
             .Drive
             .Root;
      
       //3.Upload a file
       var pathToFile = @"c:\Temp\Guide.docx";
       using (var fileStream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read))
       {
            var uploadedItem = await targetFolder
              .ItemWithPath("Guide.docx")
              .Content
              .Request()
              .PutAsync<DriveItem>(fileStream);
       }
      
      [ 1124], где

      • hostName - имя хоста семейства сайтов (например, contoso.sharepoint.com)
      • siteUrl - URL-адрес сайта сервера, например, /sites/management
    0
    ответ дан Vadim Gremyachev 18 January 2019 в 22:37
    поделиться
    Другие вопросы по тегам:

    Похожие вопросы: