/// <summary>
    /// Binds data to basic repeater.
    /// </summary>
    protected void BindData()
    {
        // Dataset = connects repeater with data source
        if (UseClassicDataset)
        {
            BasicRepeater.DataSource = SPDataSource.DataSource;

            if (!DataHelper.DataSourceIsEmpty(BasicRepeater.DataSource))
            {
                // Set proper transformations
                LoadTransformations();

                BasicRepeater.DataBind();
            }
        }
        // XSLT
        else
        {
            XmlNode caml      = SPDataSource.DataSource as XmlNode;
            string  transName = TransformationName;

            // If selected item is set
            if (SPDataSource.IsSelected && !String.IsNullOrEmpty(SelectedItemTransformationName))
            {
                transName = SelectedItemTransformationName;
            }

            // Get transformation info
            TransformationInfo ti = TransformationInfoProvider.GetTransformation(transName);

            if ((caml != null) && (ti != null))
            {
                // Check it is XSLT transformation
                if (ti.TransformationType != TransformationTypeEnum.Xslt)
                {
                    DisplayError(string.Format(GetString("sharepoint.XSL"), ti.TransformationFullName));
                    return;
                }

                try
                {
                    ltlTransformedOutput.Text = SharePointFunctions.TransformCAML(caml, ti);
                }
                catch (Exception ex)
                {
                    // Show error
                    DisplayError(string.Format(GetString("sharepoint.XSLTError") + ResHelper.Colon + " " + ex.Message));
                    DisplayError("XSLT error: " + ex.Message);
                }
            }
        }
    }
Пример #2
0
        /// <summary>
        /// Retrieves file(document or image) content of specified document from SharePoint server
        /// trough Copy web service
        /// </summary>
        /// <returns>Byte array</returns>
        private byte[] GetSPDocument()
        {
            // Get parameters from URL
            string serverUrl = SharePointServer;

            if ((serverUrl == null) || (SharePointFilePath == null))
            {
                return(null);
            }

            // Prepare valid server address
            if (!serverUrl.StartsWithCSafe("http://") && !serverUrl.StartsWithCSafe("https://"))
            {
                serverUrl = "http://" + serverUrl;
            }
            serverUrl = serverUrl.TrimEnd('/');

            // Complete URL to path
            string fileUrl = serverUrl + "/" + SharePointFilePath;

            // Download file from SharePoint
            byte[]    fileContents = null;
            WebClient wc           = new WebClient();

            try
            {
                // Try download file
                wc.Credentials = SharePointFunctions.GetSharePointCredetials();
                fileContents   = wc.DownloadData(fileUrl);
            }
            catch (Exception ex)
            {
                // Log exception to Event log
                EventLogProvider ep = new EventLogProvider();
                ep.LogEvent("GetSharePointFile", "GetItem", ex);
            }

            return(fileContents);
        }
    /// <summary>
    /// Retrieves data from SharePoint server using web services.
    /// </summary>
    /// <returns>Dataset or XmlNode</returns>
    protected object GetSharePointData()
    {
        #region "Prepare credentials for authentication"

        ICredentials credentials = null;

        // If there are data in username or password use it for authentication
        if (String.IsNullOrEmpty(Username) && String.IsNullOrEmpty(Password))
        {
            credentials = SharePointFunctions.GetSharePointCredetials();
        }
        else
        {
            credentials = SharePointFunctions.GetSharePointCredetials(Username, Password, useBase64Encoding);
        }

        #endregion


        #region "Retrieve SharePoint data"

        string serviceURL = SPServiceURL;

        // If not direct web service specified, determine web service URL by mode
        if (!serviceURL.EndsWithCSafe(".asmx", true))
        {
            // Remove trailing slash
            serviceURL = serviceURL.TrimEnd('/');

            switch (Mode.ToLowerCSafe())
            {
            case MODE_LIST_ITEMS:
            case MODE_SITE_LISTS:
                serviceURL += "/_vti_bin/lists.asmx";
                break;

            case MODE_PICTURE_LIBRARIES:
            case MODE_PICTURE_LIBRARY_ITEMS:
                serviceURL += "/_vti_bin/imaging.asmx";
                break;
            }
        }

        // Query web service
        try
        {
            Lists   spLists;
            Imaging spImaging;

            switch (Mode.ToLowerCSafe())
            {
            // Load list items
            case MODE_LIST_ITEMS:

                // Instantiate Lists service
                spLists             = new Lists(serviceURL);
                spLists.Credentials = credentials;

                camlData = LoadListItems(spLists, ListName);
                break;

            // Load site lists
            case MODE_SITE_LISTS:
                // Instantiate Lists service
                spLists             = new Lists(serviceURL);
                spLists.Credentials = credentials;

                // Get all SharePoint lists
                camlData = spLists.GetListCollection();
                break;

            // Load picture libraries
            case MODE_PICTURE_LIBRARIES:
                // Instantiate imaging service
                spImaging             = new Imaging(serviceURL);
                spImaging.Credentials = credentials;

                // Get picture libraries
                camlData = spImaging.ListPictureLibrary();
                break;

            // Load picture library items
            case MODE_PICTURE_LIBRARY_ITEMS:
                // Instantiate imaging service
                spImaging             = new Imaging(serviceURL);
                spImaging.Credentials = credentials;

                // Show error if library name empty
                if (String.IsNullOrEmpty(ListName))
                {
                    DisplayError(ResHelper.GetString("SharePoint.picslibunspecified"));
                    break;
                }

                // Get pictures in libraries, directly (not in folder)
                camlData = spImaging.GetListItems(ListName, null);
                break;
            }
        }
        catch (Exception ex)
        {
            DisplayError(ResHelper.GetString("sharepoint.erroradta") + ResHelper.Colon + " " + ex.Message);
        }


        // No data
        if (camlData == null)
        {
            return(null);
        }

        #endregion


        #region "Prepare data"

        // Use of XSLT transformation
        if (!UseClassicDataset)
        {
            dataSource = camlData;
        }
        // Use of classic dataset
        else
        {
            // Prepare dataset
            DataSet ds = new DataSet();

            // If datset fields are specified
            if (!String.IsNullOrEmpty(Fields))
            {
                DataTable dt = ds.Tables.Add();

                string[] fields = Fields.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string field in fields)
                {
                    string currentField = field.Trim();
                    if (!string.IsNullOrEmpty(currentField))
                    {
                        try
                        {
                            dt.Columns.Add(field.Trim());
                        }
                        catch (DuplicateNameException)
                        {
                            mErrorMessage = ResHelper.GetString("sharepoint.duplicateField");
                        }
                    }
                }

                XmlNodeList records = GetDataRecords(camlData);

                List <string> fieldsCheck = new List <string>();
                fieldsCheck.AddRange(fields);

                // Load items to dataset
                foreach (XmlNode record in records)
                {
                    DataRow dr = dt.NewRow();

                    // Add fields
                    foreach (string field in fields)
                    {
                        string currentField = field.Trim();
                        if (!string.IsNullOrEmpty(currentField))
                        {
                            XmlAttribute attr = record.Attributes[currentField];
                            if (attr != null)
                            {
                                dr[currentField] = attr.Value;

                                // At least one record has the field
                                fieldsCheck.Remove(field);
                            }
                        }
                    }

                    dt.Rows.Add(dr);
                }

                // None of retrieved records has fields
                if (fieldsCheck.Count > 0)
                {
                    DisplayError(String.Format(ResHelper.GetString("sharepoint.fieldnotFound"), fieldsCheck[0]));
                    return(null);
                }

                // Set daatsource
                dataSource = ds;
            }
            // No fields specified, use all fields
            else
            {
                // Only if CAML contains data record, otherwise dataset would contain wrong values
                if (HasData)
                {
                    camlData.InnerXml = SpecialCharsRegex.Replace(camlData.InnerXml, "_");
                    XmlNodeReader rd = new XmlNodeReader(camlData);
                    ds.ReadXml(rd);
                    rd.Close();

                    switch (Mode.ToLowerCSafe())
                    {
                    // Use last datatable as datasource
                    case MODE_LIST_ITEMS:

                        dataSource = ds.Tables[ds.Tables.Count - 1].DefaultView;
                        break;

                    // Filter hidden lists in dataset
                    case MODE_SITE_LISTS:
                        // Dataset structure changes based on xml, try to use last data table
                        DataTable dt = ds.Tables[ds.Tables.Count - 1];

                        // Show only visible lists
                        dt.DefaultView.RowFilter = "Hidden = 'False'";
                        dataSource = dt.DefaultView;
                        break;

                    // Dateset with picture libraries
                    case MODE_PICTURE_LIBRARIES:
                        dataSource = ds.Tables[ds.Tables.Count - 1].DefaultView;
                        break;

                    // Dataset with pictures
                    case MODE_PICTURE_LIBRARY_ITEMS:
                        dataSource = ds.Tables[ds.Tables.Count - 1].DefaultView;
                        break;
                    }
                }
            }
        }

        #endregion


        ShowRawResponse(camlData);

        return(dataSource);
    }