Esempio n. 1
0
File: sites.py Progetto: mmg1/SOSA
def list_sites(username, password):
    ctx_auth = AuthenticationContext(url)
    if ctx_auth.acquire_token_for_user(username, password):
        request = ClientRequest(ctx_auth)
        options = RequestOptions("{0}/_api/web/".format(url))
        options.set_header('Accept', 'application/json')
        options.set_header('Content-Type', 'application/json')
        data = request.execute_query_direct(options)
        s = json.loads(data.content)
        web_title = s['Title']
        return "Web title: " + web_title
    else:
        return ctx_auth.get_last_error()
        'X-HTTP-Method': 'MERGE'
    }
    request.execute_query_direct(request_url=request_url, headers=headers, data=item_payload)
    print "Task has been successfully [updated]"


def delete_list_item(web_url, ctx_auth, list_title, item_id):
    """Delete list item example"""
    request = ClientRequest(web_url, ctx_auth)
    request_url = "{0}/_api/web/lists/getbyTitle('{1}')/items({2})".format(web_url, list_title, item_id)
    print "Deleting list item..."
    headers = {
        'IF-MATCH': '*',
        'X-HTTP-Method': 'DELETE'
    }
    request.execute_query_direct(request_url=request_url, headers=headers)
    print "Task has been successfully [deleted]"


if __name__ == '__main__':
    context_auth = AuthenticationContext(url=settings['url'])
    if context_auth.acquire_token_for_user(username=settings['username'], password=settings['password']):

        read_list_items(settings['url'], context_auth, "Tasks")
        task_item = create_list_item(settings['url'], context_auth, "Tasks")
        update_list_item(settings['url'], context_auth, "Tasks", task_item['Id'])
        delete_list_item(settings['url'], context_auth, "Tasks", task_item['Id'])

    else:
        print context_auth.get_last_error()
Esempio n. 3
0
        print "Task '{0}' has been created".format(
            task_item.properties["Title"])


def generate_contacts(context):
    contacts_list = ctx.web.lists.get_by_title("Contacts")
    fake = Faker()
    for idx in range(0, 1):
        name = fake.name()
        contact_properties = {
            '__metadata': {
                'type': 'SP.Data.ContactsListItem'
            },
            'Title': name
        }
        contact_item = contacts_list.add_item(contact_properties)
        context.execute_query()
        print "Contact '{0}' has been created".format(
            contact_item.properties["Title"])


if __name__ == '__main__':
    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_user(username=settings['username'],
                                       password=settings['password']):
        ctx = ClientContext(settings['url'], ctx_auth)
        generate_tasks(ctx)
        #generate_contacts(ctx)
    else:
        print ctx_auth.get_last_error()
Esempio n. 4
0
                                 headers=headers,
                                 data=item_payload)
    print "Task has been successfully [updated]"


def delete_list_item(web_url, ctx_auth, list_title, item_id):
    """Delete list item example"""
    request = ClientRequest(web_url, ctx_auth)
    request_url = "{0}/_api/web/lists/getbyTitle('{1}')/items({2})".format(
        web_url, list_title, item_id)
    print "Deleting list item..."
    headers = {'IF-MATCH': '*', 'X-HTTP-Method': 'DELETE'}
    request.execute_query_direct(request_url=request_url, headers=headers)
    print "Task has been successfully [deleted]"


if __name__ == '__main__':
    context_auth = AuthenticationContext(url=settings['url'])
    if context_auth.acquire_token_for_user(username=settings['username'],
                                           password=settings['password']):

        read_list_items(settings['url'], context_auth, "Tasks")
        task_item = create_list_item(settings['url'], context_auth, "Tasks")
        update_list_item(settings['url'], context_auth, "Tasks",
                         task_item['Id'])
        delete_list_item(settings['url'], context_auth, "Tasks",
                         task_item['Id'])

    else:
        print context_auth.get_last_error()
Esempio n. 5
0
    groupName = "Orders Approvers221"
    groups = ctx.web.site_groups
    groupProperties = {'__metadata': {'type': 'SP.Group'}, 'Title': groupName}
    group = groups.add(groupProperties)
    ctx.execute_query()
    print "Group : {0} has been created".format(group.properties["Title"])

    "Retrieve group users"
    users = group.users
    ctx.load(users)
    ctx.execute_query()
    for user in users:
        print "User : {0}".format(user.properties["Title"])

    "Remove a group"
    groups.remove_by_login_name(groupName)
    ctx.execute_query()
    print "Group : {0} has been deleted".format(groupName)


if __name__ == '__main__':
    ctxAuth = AuthenticationContext(url=settings['url'])
    if ctxAuth.acquire_token_for_user(username=settings['username'], password=settings['password']):
        ctx = ClientContext(settings['url'], ctxAuth)

        read_groups(ctx)
        #crudGroup(ctx)

    else:
        print ctxAuth.get_last_error()
    """Read a folder example"""
    list_obj = ctx.web.lists.get_by_title(listTitle)
    folder = list_obj.root_folder
    ctx.load(folder)
    ctx.execute_query()
    print "List url: {0}".format(folder.properties["ServerRelativeUrl"])

    files = folder.files
    ctx.load(files)
    ctx.execute_query()
    for cur_file in files:
        print "File name: {0}".format(cur_file.properties["Name"])

    folders = ctx.web.folders
    ctx.load(folders)
    ctx.execute_query()
    for folder in folders:
        print "Folder name: {0}".format(folder.properties["Name"])


if __name__ == '__main__':
    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_user(username=settings['username'], password=settings['password']):
        ctx = ClientContext(settings['url'], ctx_auth)

        listTitle = "Documents"
        read_folder_and_files()

    else:
        print ctx_auth.get_last_error()