SharePoint


List of examples:

Creating a SharePoint connection



// Specifies the parameters for the SharePoint connection
// The siteUrl variable represents the URL of your configured SharePoint site
string siteUrl = "https://mycompany.sharepoint.com";
string userName = "yourSharePointUsername";
string password = "yourSharePointPassword";

// Creates a new SharePoint connection object
SharePointConnectionInfo newConnection = new SharePointConnectionInfo();

// Sets the properties of the connection
newConnection.SharePointConnectionSiteUrl = siteUrl;
newConnection.SharePointConnectionSiteID = SiteContext.CurrentSiteID;
newConnection.SharePointConnectionDisplayName = "New connection";
newConnection.SharePointConnectionName = "NewConnection";
newConnection.SharePointConnectionUserName = userName;
newConnection.SharePointConnectionPassword = password;

// Saves the SharePoint connection to the database
SharePointConnectionInfoProvider.SetSharePointConnectionInfo(newConnection);

> Back to list of examples

Updating a SharePoint connection



// Gets the SharePoint connection
SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("NewConnection", SiteContext.CurrentSiteID);

// Updates the properties of the connection
connection.SharePointConnectionDisplayName = connection.SharePointConnectionDisplayName.ToLowerCSafe();

// Saves the updated connection to the database
SharePointConnectionInfoProvider.SetSharePointConnectionInfo(connection);

> Back to list of examples

Deleting a SharePoint connection



// Gets the SharePoint connection
SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("NewConnection", SiteContext.CurrentSiteID);

if (connection != null)
{
    // Deletes the SharePoint connection
    SharePointConnectionInfoProvider.DeleteSharePointConnectionInfo(connection);
}

> Back to list of examples

Retrieving SharePoint lists



// Gets the SharePoint connection
SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("NewConnection", SiteContext.CurrentSiteID);

// Converts the SharePointConnectionInfo into a connection data object
SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

// Gets the list service implementation
ISharePointListService listService = SharePointServices.GetService<ISharePointListService>(connectionData);

// Chooses the SharePoint list type that will be retrieved
// You can use an enumeration or template identifier (listed in http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx)
int listType = SharePointListType.ALL;

try
{
    // Gets all lists of the specified type (all list types are retrieved in this case)
    DataSet results = listService.GetLists(listType);

    if ((results.Tables.Count == 0) || (results.Tables[0].Rows.Count == 0))
    {
        // No lists were retrieved from the SharePoint server
    }
}
catch (Exception ex)
{
    // The retrieval of the lists ended with an exception
    // Logs the exception to the Xperience event log
    Service.Resolve<IEventLogService>().LogException("SharePoint API Example", "EXCEPTION", ex);
}

> Back to list of examples

Retrieving SharePoint list items



// Specifies the list name
string listName = "SharePointList";

// Gets the SharePoint connection
SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("NewConnection", SiteContext.CurrentSiteID);

// Converts the SharePointConnectionInfo into a connection data object
SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

// Gets the list service implementation
ISharePointListService listService = SharePointServices.GetService<ISharePointListService>(connectionData);

try
{
    // Gets the specified list's items
    DataSet results = listService.GetListItems(listName);

    if ((results.Tables.Count == 0) || (results.Tables[0].Rows.Count == 0))
    {
        // No list items were retrieved from the SharePoint server 
    }
}
catch (Exception ex)
{
    // The retrieval of the list items ended with an exception
    // Logs the exception to the Xperience event log
    Service.Resolve<IEventLogService>().LogException("SharePoint API Example", "EXCEPTION", ex);
}

> Back to list of examples

Retrieving a file from a SharePoint server



// Specifies the relative path of the file
string filePath = "/Picture library/picture.jpg";

// Gets the SharePoint connection
SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("NewConnection", SiteContext.CurrentSiteID);

// Converts the SharePointConnectionInfo into a connection data object
SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

// Gets the file service implementation
ISharePointFileService fileService = SharePointServices.GetService<ISharePointFileService>(connectionData);

// Gets the SharePoint URL provider
SharePointUrlProvider sharePointUrlProvider = new SharePointUrlProvider(connection.SharePointConnectionName);

try
{
    // Gets the file object
    ISharePointFile file = fileService.GetFile(filePath);

    // Gets the file's metadata
    string extension = file.Extension;

    // Gets a stream of the file's binary content
    Stream fileContentStream = file.GetContentStream();

    // Gets a byte array of the file's binary content
    byte[] fileContentBytes = file.GetContentBytes();

    // Gets the URL of the file
    string fileUrl = sharePointUrlProvider.GetSharePointFileUrl(file.ServerRelativeUrl);
}
catch (Exception ex)
{
    // The retrieval of the file ended with an exception
    // Logs the exception to the Xperience event log
    Service.Resolve<IEventLogService>().LogException("SharePoint API Example", "EXCEPTION", ex);
}

> Back to list of examples