/// <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); }
/// <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> /// 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); }