CAML 라이브러리

ClientContext를 사용하기 위해, 우리는 우리의 프로젝트에 두 개의 DLL 참조를 추가해야합니다. Microsoft.SharePoint.Client.dll 및 Microsoft.SharePoint.Client.Runtime.dll. 방법이 게시물에서 우리는 배울 것이다 :

CAML있는 ListItemCollection으로 SharePoint 문서 라이브러리에서 획득
에 SharePoint 문서 라이브러리에 문서를 업로드 할
SharePoint 문서 라이브러리에서 다운로드 파일을
CAML하여 SharePoint 문서 라이브러리에서있는 ListItemCollection을 얻기 위해
다음과 같이 우리가있는 ListItemCollection를 얻을 수 있습니다 :


= GetListItemCollectionFromSP가 많은 ListItems있는 ListItemCollection ( "FileLeafRef"DocumentName를 "텍스트",. 1)
GetListItemCollectionFormSP 메소드가 항목의 목록을 얻기 위해 사용될 수있는, 상기 제 1 파라미터 이름 - 이름의 FieldRef 번째 파라미터 값 =의 FieldRef 값, 제 세 개의 매개 변수를 입력 - 값의 유형은 마지막 매개 변수의 ROWLIMIT입니다 - 최대 레코드 수는 반환됩니다.

개인 정적있는 ListItemCollection GetListItemCollectionFromSP (문자열 이름, 문자열 값 문자열 유형의 INT ROWLIMIT)
{
    있는 ListItemCollection 많은 ListItems = NULL;
    사용 (ClientContext clientContext는 새로운 ClientContext = (SITEURL))
    {
        목록 documentsList = clientContext.Web.Lists.GetByTitle (documentListName);
        CamlQuery camlQuery = 새로운 CamlQuery (); ;
        camlQuery.ViewXml =
        @ "<보기>
        <질의>
        <>
        <식>
        <의 FieldRef 이름 = '"+ 이름 + @' '/>
        <값 유형 =' "+ 유형 +" '> "+ 값 + @" </ 값>
        </ 식>
        </ 어디>                   
        <ROWLIMIT 매> "rowLimit.ToString + () + @"</를 ROWLIMIT>
        </ 검색어>
        </보기> ";
        많은 ListItems = documentsList.GetItems (camlQuery)
        ClientContext.Load (documentsList)
        ClientContext.Load합니다 (많은 ListItems);
        clientContext.ExecuteQuery는 ();
    }
    많은 ListItems를 반환;
}
SharePoint 문서 라이브러리에 문서를 업로드
설명 필드가 "코어에 설정된 이러한 ClientContext 등에 의해 문서에 대한 메타 데이터 정보를 업데이트하는 동안 본 실시 예에있어서, 우리는 셰어 문서 라이브러리에 문서를 업로드 할 필요가있다. 다음과 같이 코드 "의 내용은 다음과 같습니다

정적 무효 UploadDocument 공개 (SITEURL 문자열, 문자열 documentListName, documentListURL 문자열, 문자열에 DocumentName, 바이트 [] documentStream)
{
    사용 (= ClientContext 새로운 새로운 ClientContext ClientContext (SITEURL))
    {
        // 가져 오기 문서 라이브러리
        목록 documentsList = clientContext.Web.Lists.GetByTitle (documentListName)
        VAR의 fileCreationInformation 새로운 새 fileCreationInformation = ();
        // 지정된 콘텐츠 바이트 [] 배열 여기 documentStream
        fileCreationInformation.Content = documentStream;
        // 있도록 문서 커버
        fileCreationInformation.Overwrite = TRUE로;
        // 업로드 URL 주소
        fileCreationInformation.Url = SITEURL + + documentListURL에 DocumentName;
        = DocumentsList.RootFolder.Files.Add을 UploadFile Microsoft.SharePoint.Client.File (fileCreationInformation)
        // 업데이트 메타 정보 "설명"라는 필드가 표시되는 필드 소위 "Description0"
        uploadFile.ListItemAllFields [ "Description0"] = "핵심";
        uploadFile.ListItemAllFields.Update ();
        clientContext.ExecuteQuery ();
    }
}
은 SharePoint 문서 라이브러리에서 문서 다운로드
우리가 문서를 다운로드하려면 다음 코드를 사용 할 수 있습니다

스트림 DownloadDocument 정적 대중에게 (SITEURL 문자열, 문자열에 DocumentName)
{
    을 ListItem = GetDocumentFromSP 항목 (DocumentName를)
    IF (! 항목 = NULL)
    {
        사용 (= ClientContext 새로운 새로운 ClientContext ClientContext (SITEURL))
        {
            FileInformation FINFO = Microsoft.SharePoint.Client.File .OpenBinaryDirect (ClientContext, 아이템 [ "FileRef"] 있으며, toString는 ().)
            ; fInfo.Stream를 반환
        }
    }
    는 null 단계;
}
개인 정적 GetDocumentFromSP을 ListItem (문자열에 DocumentName)
{
    //이 방법은 상술하고 셰어 행 문서 라이브러리 컬렉션에있는 항목의 목록을 얻을 수
    있는 ListItemCollection 많은 ListItems = GetListItemCollectionFromSP ( "FileLeafRef" , DocumentName를, "텍스트", 1);
    반환 많은 ListItems [0] (= NULL && listItems.Count 많은 ListItems == 1!.) : NULL;?
}
호 :

정적 문자열 SITEURL = "HTTP : // sp2010u / 그것은";
정적 문자열 documentListName = "共享文档";
 
정적 무효 메인 (문자열 []에 args)

    UploadTest ();
    DownloadTest ();
    Console.ReadLine ();
}
개인 정적 무효 DownloadTest ()
{
    downloadDocumentName = "중단 2.pptx"스트링;
    스트림 S = DownloadDocument (SITEURL, downloadDocumentName);
 
    문자열 저금 = @ "C : \"+ downloadDocumentName;
    //创建一个写入流
    하여 FileStream writeStream = 새로운하여 FileStream (저금, FileMode.Create, FileAccess.Write);
    //写入到该流
    ReadWriteStream (S, writeStream);
    Console.WriteLine ( "完成下载!");


{
    문자열 uploadDocumentName = "수업 1.pptx";
    문자열 openFrom @ = "C : \"+ uploadDocumentName;
    IF (System.IO.File.Exists (openFrom)!)
    {
        투사 ArgumentException이 새로운 새로운합니다 (및 String.format ( "{0} ; "openFrom)"openFrom ")가 존재하지 않는다
    }
 
    파일 스트림 fstream = System.IO.File.OpenRead (openFrom)
    바이트 [] = 새로운 새 바이트 목차 [fStream.Length]
    fStream.Read (내용량 0 (지능 ) fStream.Length)
    // "공유 문서"문서 라이브러리에 설정된 "마케팅 계획"문서 업로드
    UploadDocument (SITEURL, documentListName "/ 공유 % 20Documents / % E9의 % 94 % 80 % E5의 % 94 % AE의 %의 E8 보조 기기 %의 A1 %의 E5의 % 88 % 92 % / "uploadDocumentName 목차)
    Console.WriteLine는 ("업로드가 완료 ") ;!
    fStream.Close ();
 
}
// readStream 당신이 읽을 필요가 스트림입니다
// writeStream是你需要写入的流
개인 정적 무효 ReadWriteStream (트림 readStream 스트림 writeStream)
{
    INT 길이 = 256;
    바이트 [완충액 = 새로운 바이트 [길이];
    INT bytesRead = readStream.Read (버퍼, 0, 길이);
    //写入要求的字节
    동안 (bytesRead> 0)
    {
        writeStream.Write (버퍼 0 bytesRead);
        bytesRead = readStream.Read (버퍼, 0, 길이);
    }
    readStream.Close ();
    writeStream.Close ();
}

추천

출처www.cnblogs.com/xdanny/p/11569733.html