Beispiel #1
0
    /// <summary>
    /// Gets metadata about all lists stored on the SharePoint server using "My new connection". Called when the "Get all lists" button is clicked.
    /// Expects the CreateSharePointConnection method to be run first.
    /// </summary>
    private void GetAllLists()
    {
        // Get the SharePoint connection from DB
        SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("MyNewConnection", SiteContext.CurrentSiteID);

        if (connection == null)
        {
            throw new CMSAPIExampleException("SharePoint connection 'My new connection' was not found.");
        }

        // Convert SharePointConnectionInfo object into connection data
        SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

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

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

        try
        {
            // Get all lists of 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))
            {
                throw new CMSAPIExampleException("No lists were retrieved from SharePoint server.");
            }
        }
        catch (Exception ex)
        {
            throw new CMSAPIExampleException(ex.Message);
        }
    }
    /// <summary>
    /// Loads options for combo box from SharePoint.
    /// Fills the options directly in the combo box.
    /// </summary>
    private void LoadListsFromSharePoint()
    {
        drpSharePointLists.DropDownList.Items.Clear();
        int connectionID   = SharePointConnectionID;
        var connectionInfo = SharePointConnectionInfoProvider.GetSharePointConnectionInfo(connectionID);

        if (connectionInfo == null)
        {
            return;
        }

        DataSet lists = null;

        try
        {
            ISharePointListService service = SharePointServices.GetService <ISharePointListService>(connectionInfo.ToSharePointConnectionData());
            lists = service.GetLists(SharePointListType);
        }
        catch (Exception ex)
        {
            mShowConnectionWarning = true;
            EventLogProvider.LogWarning("SharePoint", "GETSHAREPOINTLISTS", ex, SiteContext.CurrentSiteID, String.Empty);
        }

        DataHelper.ForEachRow(lists, AddOptionToComboBoxFromRow);
    }
Beispiel #3
0
    /// <summary>
    /// Gets specified file from SharePoint server using "My new connection". Called when the "Get file" button is clicked.
    /// Expects the CreateSharePointConnection method to be run first.
    /// </summary>
    private void GetFile()
    {
        // Verify the file path has been provided.
        string filePath = txtFilePath.Text;

        if (String.IsNullOrWhiteSpace(filePath))
        {
            throw new CMSAPIExampleException("Empty value for File type is not allowed.");
        }

        // Get the SharePoint connection from DB
        SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("MyNewConnection", SiteContext.CurrentSiteID);

        if (connection == null)
        {
            throw new CMSAPIExampleException("SharePoint connection 'My new connection' was not found.");
        }

        // Convert SharePointConnectionInfo object into connection data
        SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

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

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

            // Get file metadata
            string extension = file.Extension;

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

            // Get byte array of file's binary content
            byte[] fileContentBytes = file.GetContentBytes();
        }
        catch (Exception ex)
        {
            throw new CMSAPIExampleException(ex.Message);
        }
    }
Beispiel #4
0
    /// <summary>
    /// Gets all items of specified SharePoint list using "My new connection". Called when the "Get list items" button is clicked.
    /// Expects the CreateSharePointConnection method to be run first.
    /// </summary>
    private void GetListItems()
    {
        // Verify the list name has been provided.
        string listName = txtListName.Text;

        if (String.IsNullOrWhiteSpace(listName))
        {
            throw new CMSAPIExampleException("Empty value for List name is not allowed.");
        }

        // Get the SharePoint connection from DB
        SharePointConnectionInfo connection = SharePointConnectionInfoProvider.GetSharePointConnectionInfo("MyNewConnection", SiteContext.CurrentSiteID);

        if (connection == null)
        {
            throw new CMSAPIExampleException("SharePoint connection 'My new connection' was not found.");
        }

        // Convert SharePointConnectionInfo object into connection data
        SharePointConnectionData connectionData = connection.ToSharePointConnectionData();

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

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

            if ((results.Tables.Count == 0) || (results.Tables[0].Rows.Count == 0))
            {
                throw new CMSAPIExampleException("No list's items were retrieved from SharePoint server.");
            }
        }
        catch (Exception ex)
        {
            throw new CMSAPIExampleException(ex.Message);
        }
    }
    /// <summary>
    /// Tests SharePoint connection using configuration currently filled in the form.
    /// </summary>
    private void TestConnection()
    {
        try
        {
            ISharePointSiteService siteService = SharePointServices.GetService <ISharePointSiteService>(GetConnectionData());

            siteService.GetSiteUrl();

            DisplayConnectionStatus(GetString("sharepoint.testconnection.success"));
        }
        catch (SharePointServiceFactoryNotSupportedException)
        {
            // No service factory for given SharePoint version
            DisplayConnectionStatus(GetString("sharepoint.versionnotsupported"), false);
        }
        catch (SharePointServiceNotSupportedException)
        {
            // No ISiteService implementation for SharePoint version
            DisplayConnectionStatus(GetString("sharepoint.testconnectionnotsupported"), false);
        }
        catch (SharePointConnectionNotSupportedException)
        {
            // The ISiteService implementation rejected connection data
            DisplayConnectionStatus(GetString("sharepoint.invalidconfiguration"), false);
        }
        catch (SharePointCCSDKException ex)
        {
            var message = string.Format(GetString("sharepoint.ccsdk.idcrl.msoidclilerror"), DocumentationHelper.GetDocumentationTopicUrl("sharepoint_online_connecting"));
            DisplayConnectionStatus(message, false, true);
            EventLogProvider.LogException("SharePoint", "TESTCONNECTION", ex);
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                // Connection established, but response indicates error condition
                if (ex.Message.Contains("401"))
                {
                    // Unauthorized.
                    DisplayConnectionStatus(GetString("sharepoint.protocolerror.unauthorized"), false);
                }
                else if (ex.Message.Contains("404"))
                {
                    // SharePoint instance not found on given URL
                    DisplayConnectionStatus(GetString("sharepoint.protocolerror.notfound"), false);
                }
                else
                {
                    // Some other protocol error
                    DisplayConnectionStatus(GetString("sharepoint.protocolerror"), false);
                }
            }
            else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            {
                // Given site URL does not have a resolution
                DisplayConnectionStatus(GetString("sharepoint.nameresolutionfailure"), false);
            }
            else
            {
                DisplayConnectionStatus(GetString("sharepoint.unknownerror"), false);
                EventLogProvider.LogException("SharePoint", "TESTCONNECTION", ex);
            }
        }
        catch (Exception ex)
        {
            DisplayConnectionStatus(GetString("sharepoint.unknownerror"), false);
            EventLogProvider.LogException("SharePoint", "TESTCONNECTION", ex);
        }
    }
    /// <summary>
    /// Retrieves requested data from SharePoint server.
    /// Displays error message if not able to do so and returns null.
    /// </summary>
    /// <returns>Requested data, or null if something goes wrong.</returns>
    private DataSet GetSharePointData()
    {
        // Data does not originate from cache
        DataRetrievedFromServer = true;
        DataSet result = null;

        if (ConnectionInfo == null)
        {
            DisplayError(GetString("sharepoint.wp.datasource.noconnection"));

            return(null);
        }

        try
        {
            ISharePointListService listService = SharePointServices.GetService <ISharePointListService>(ConnectionInfo.ToSharePointConnectionData());

            if (Mode == MODE.LISTS)
            {
                result = listService.GetLists(ListType);
            }
            else
            {
                result = listService.GetListItems(ListTitle, FolderServerRelativeUrl, View, ListItemsSelection);
            }

            if (result != null && (result.Tables.Count == 0 || result.Tables[0].Columns.Count == 0))
            {
                result = null;
            }
        }
        catch (SharePointServiceFactoryNotSupportedException)
        {
            // No service factory for given SharePoint version
            DisplayError(GetString("sharepoint.versionnotsupported"));
        }
        catch (SharePointServiceNotSupportedException)
        {
            // No ISharePointListService implementation for SharePoint version
            DisplayError(GetString("sharepoint.listsnotsupported"));
        }
        catch (SharePointConnectionNotSupportedException)
        {
            // The ISharePointListService implementation rejected connection data
            DisplayError(GetString("sharepoint.invalidconfiguration"));
        }
        catch (SharePointCCSDKException ex)
        {
            DisplayError(GetString("sharepoint.ccsdk.idcrl.msoidclilerror"));
            EventLogProvider.LogException("SharePoint", "DATASOURCE", ex);
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                // Connection established, but response indicates error condition
                if (ex.Message.Contains("401"))
                {
                    // Unauthorized.
                    DisplayError(GetString("sharepoint.protocolerror.unauthorized"));
                }
                else if (ex.Message.Contains("404"))
                {
                    // SharePoint instance not found on given URL
                    DisplayError(GetString("sharepoint.protocolerror.notfound"));
                }
                else
                {
                    // Some other protocol error
                    DisplayError(GetString("sharepoint.protocolerror"));
                }
            }
            else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            {
                // Given site URL does not have a resolution
                DisplayError(GetString("sharepoint.nameresolutionfailure"));
            }
            else
            {
                DisplayError(GetString("sharepoint.unknownerror"));
                EventLogProvider.LogException("SharePoint", "DATASOURCE", ex);
            }
        }
        catch (SharePointServerException ex)
        {
            DisplayError(String.Format(GetString("sharepoint.servererror"), ex.Message));
            EventLogProvider.LogException("SharePoint", "DATASOURCE", ex);
        }
        catch (Exception ex)
        {
            DisplayError(GetString("sharepoint.unknownerror"));
            EventLogProvider.LogException("SharePoint", "DATASOURCE", ex);
        }

        return(result);
    }