본문 바로가기

SharePoint/SharePoint 개발

Client Object Model - 데이터 삭제하기

Client Object Model 로 데이터 삭제 하기


JohnHolliday.Caml.Net.dll 을 참조하여 CAML 쿼리를 사용 했습니다.

SP는 Microsoft.SharePoint.Client 를 using 으로 지정하였습니다.

using SP = Microsoft.SharePoint.Client;


string whereCondition = string.Empty;

string orderBy = string.Empty;

string viewFields = string.Empty;

using (SP.ClientContext ClientCon = new SP.ClientContext("http://mysite"))

{

    SP.Web site = ClientCon.Web;

    NetworkCredential Credential = new NetworkCredential(Admin, Password, Domain);

    ClientCon.Credentials = Credential;

    SP.List list = site.Lists.GetByTitle("mylist");

    ClientCon.Load(list);

    ClientCon.ExecuteQuery();

    SP.CamlQuery cmQuery = new Microsoft.SharePoint.Client.CamlQuery();

    

    //Contet 필드가 null 아닌 데이터를 찾습니다.

    whereCondition = CAML.IsNotNull(CAML.FieldRef("Content"));

    whereCondition = CAML.Where(whereCondition);


    orderBy = CAML.OrderBy(CAML.FieldRef("Created", CAML.SortType.Ascending));

     //View Scope=Recursive 는 폴더안의 데이터도 모두 포함 시킨다는 것입니다.

    cmQuery.ViewXml = "<View Scope=\"Recursive\"><Query>" + orderBy + whereCondition + "</Query><RowLimit>" + list.ItemCount + "</RowLimit></View>";

    //<ViewScope='Recursive'>

    SP.ListItemCollection Delitems2 = list.GetItems(cmQuery);

    ClientCon.Load(Delitems2);

    ClientCon.ExecuteQuery();

    int count = Delitems2.Count;

    for (int i = 0; i < count; i++)

    {

        Delitems2[0].DeleteObject();

        ClientCon.ExecuteQuery();

    }

}