1

I need to access a list in office 365 from a page in asp .net, this page is not in the office 365 server and I've tried various methods, for example via the web services in list.asmx but I got problems with the claim authentication, I have not found any practical example on how to do this. How can I insert an item in a list in office 365 from an asp .net site?

update - solution

you can find the solution in Accessing SharePoint Online from a Windows client but I would like to contribute with this code from http://sneakydev.blogspot.com.es/2013/05/using-c-csom-to-interact-with.html :

  Microsoft.SharePoint.Client.ClientContext context;
  context = GetContext();
 getlists();

// and in a class you put this


    // utility class for accessing office365
    private class Configuration
    {
        public static string ServiceSiteUrl = "url";
        public static string ServiceUserName = "user@site.onmicrosoft.com";
        public static string ServicePassword = "password";
    }

    static ClientContext GetContext()
    {
        var securePassword = new SecureString();
        foreach (char c in Configuration.ServicePassword)
        {
            securePassword.AppendChar(c);
        }

        var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);

        var context = new ClientContext(Configuration.ServiceSiteUrl);
        context.Credentials = onlineCredentials;

        return context;
    }

    static void GetLists()
    {
        var context = GetContext();
        var results = context.LoadQuery(context.Web.Lists.Include(list => list.Title, list => list.Id));
        context.ExecuteQuery();
        results.ToList().ForEach(x =>
        {
            Console.WriteLine("List: " + x.Title);
        });

        context.Dispose();
    }




    static void CreateProjectList()
    {
        ClientContext clientContext = GetContext();
        Web site = clientContext.Web;

        // Create the project list.
        ListCreationInformation listCreationInfo = new ListCreationInformation();
        listCreationInfo.Title = "Projects";
        listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
        List list = site.Lists.Add(listCreationInfo);

        // Add the category field to the list.
        Field catField = list.Fields.AddFieldAsXml(@"
            <Field Type='Choice' DisplayName='Category' Format='Dropdown'>
                <Default>IT</Default>
                <CHOICES>
                  <CHOICE>IT</CHOICE>
                  <CHOICE>Sales</CHOICE>
                  <CHOICE>Research and Development</CHOICE>
                  <CHOICE>CSR</CHOICE>
                </CHOICES>
            </Field>
            ", true, AddFieldOptions.DefaultValue);

        // Add list data.
        ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
        Microsoft.SharePoint.Client.ListItem listItem = list.AddItem(itemCreationInfo);
        listItem["Title"] = "New public facing website";
        listItem["Category"] = "IT";
        listItem.Update();

        listItem = list.AddItem(itemCreationInfo);
        listItem["Title"] = "New Incident Management System";
        listItem["Category"] = "IT";
        listItem.Update();

        listItem = list.AddItem(itemCreationInfo);
        listItem["Title"] = "New internal sales strategy";
        listItem["Category"] = "Sales";
        listItem.Update();

        listItem = list.AddItem(itemCreationInfo);
        listItem["Title"] = "Youtube product visibility";
        listItem["Category"] = "CSR";
        listItem.Update();

        clientContext.ExecuteQuery();
    }
}
0

You can use Sharepoint client object model to access data from sharepoint:

SecureString password = new SecureString();

 foreach (char c in password)
 {
 password.AppendChar(c);
 }

            using (var context = new ClientContext(webUrl))
            {
                context.Credentials = new SharePointOnlineCredentials(userName,password);
                List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); 
ListItem newItem = announcementsList.AddItem(itemCreateInfo); 
newItem["Title"] = "My New Item!"; 
newItem["Body"] = "Hello World!"; 
newItem.Update(); 

context.ExecuteQuery();  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.