Beispiel #1
0
        private void GetListByNameOrGuid(string listGuid, string listName)
        {
            txt_Guid.Text = "";
            txt_Name.Text = "";

            // Store loop counter for rows added to the data grid view
            int i = 0;

            // If we find a GUID, there will only be one
            bool foundGuid = false;

            // Store variables for field values
            string defaultViewUrl   = null;
            string defaultViewTitle = null;
            string viewCount        = null;
            string fieldCount       = null;
            string itemCount        = null;
            string listType         = null;
            string siteName         = null;
            string siteAddress      = null;
            string listTitle        = null;

            // Open the List form
            frm_Data_List listForm = OpenForm("", "", this.Name, 1000, 500);

            // Show the Site Name column
            listForm.dgv_Data.Columns["siteName"].Visible = true;

            // Hide the Site Address column
            listForm.dgv_Data.Columns["siteAddress"].Visible = false;

            // Loop through each row of the data grid view
            foreach (DataGridViewRow row in dgv_Data.Rows)
            {
                // If we find a GUID, there will only be one
                foundGuid = false;

                // Store the Site URL to get the Client Context
                string siteUrl = row.Cells[5].Value.ToString();

                // Select the row in the data grid view for visual effect
                row.Selected = true;

                // Do this or it won't show the selected record
                dgv_Data.Refresh();

                // Get the Client Context
                ClientContext clientContext = SharePoint.GetClient(siteUrl, frm_Main_Menu.username, frm_Main_Menu.password);

                // Store the lists
                ListCollection collList = clientContext.Web.Lists;

                // Load lists
                clientContext.Load(collList);

                // Execute the query to the server
                clientContext.ExecuteQuery();

                // Loop through each list to check against Name (i.e. Title) or GUID
                foreach (SP.List oList in collList)
                {
                    // Variable to check if we need to add a row
                    bool addRow = false;

                    // If we have a GUID, check the GUID
                    if (listGuid != "")
                    {
                        // Check the GUID
                        if (oList.Id.ToString() == listGuid)
                        {
                            // Select the cell to indicate a find
                            row.Cells[1].Selected = true;

                            // Set the variables
                            foundGuid = true;
                            addRow    = true;
                        }
                    }
                    // We are checking name
                    else
                    {
                        // Do a contains for basic "fuzzy" matching
                        if (oList.Title.ToLower().Contains(listName.ToLower()))
                        {
                            // Select the cell to indicate a find
                            row.Cells[1].Selected = true;

                            // Set the variable to add a row
                            addRow = true;
                        }
                    }

                    // Check if we are needing to add a row to the data grid view
                    if (addRow)
                    {
                        // Increment counter
                        i++;

                        // Load lists
                        clientContext.Load(oList, l => l.SchemaXml);

                        // Execute the query to the server
                        clientContext.ExecuteQuery();

                        // Set the field-related vairables
                        SetFieldVars(clientContext, oList, listForm.dgv_Data, ref fieldCount);

                        // Set the view-related variables
                        SetViewVars(clientContext, oList, listForm.dgv_Data, ref defaultViewUrl, ref defaultViewTitle, ref viewCount);

                        // Set other variables
                        itemCount   = oList.ItemCount.ToString();
                        listType    = GetBaseTypeDescription(oList.BaseType);
                        siteAddress = row.Cells[5].Value.ToString();
                        listTitle   = oList.Title;

                        // Find the full Site Name that the List belongs to
                        if (row.Cells[2].Value != null)
                        {
                            siteName = row.Cells[2].Value.ToString() + " --> " + row.Cells[1].Value.ToString();
                        }
                        else
                        {
                            siteName = row.Cells[1].Value.ToString();
                        }

                        // Add a row to the data grid view
                        listForm.AddRow
                        (
                            i.ToString(),
                            siteName,
                            siteAddress,
                            listTitle,
                            oList.Description,
                            listType,
                            oList.EnableVersioning,
                            oList.MajorVersionLimit,
                            defaultViewTitle,
                            fieldCount,
                            viewCount,
                            itemCount,
                            !oList.DisableGridEditing,
                            oList.Id.ToString(),
                            oList.Created.ToString(),
                            defaultViewUrl,
                            oList.SchemaXml.ToString()
                        );

                        lbl_Row_Count.Text = i.ToString() + " record(s) found";
                        lbl_Row_Count.Refresh();

                        // Show the form if it is still invisible
                        if (listForm.Visible == false)
                        {
                            listForm.Show();
                        }

                        // Refresh it for visual effects
                        listForm.Refresh();

                        // Leave if the addition was the result of a GUID match
                        if (foundGuid == true)
                        {
                            break;
                        }
                    }
                }

                // Leave if the addition was the result of a GUID match
                if (foundGuid == true)
                {
                    break;
                }
            }

            if (i > 0)
            {
                if (foundGuid == true)
                {
                    MessageBox.Show("Found list '" + listTitle + "' with GUID " + listGuid);
                }
                else
                {
                    MessageBox.Show("Found " + i + " list(s) matching the name '" + listName + "'");
                }
            }
            else
            {
                MessageBox.Show("Could not find a match");
            }
        }
Beispiel #2
0
        private void AddLists(string siteName, string siteUrl, frm_Data_List listForm)
        {
            ClientContext  clientContext = SharePoint.GetClient(siteUrl, frm_Main_Menu.username, frm_Main_Menu.password);
            ListCollection collList      = SharePoint.GetLists(clientContext);

            // Initialise row counter
            int i = 0;

            foreach (SP.List oList in collList)
            {
                // Load lists
                clientContext.Load(oList, l => l.SchemaXml);

                // Execute the query to the server
                clientContext.ExecuteQuery();

                string defaultViewUrl   = null;
                string defaultViewTitle = null;
                string viewCount        = null;
                string fieldCount       = null;
                string itemCount        = oList.ItemCount.ToString();
                string listType         = GetBaseTypeDescription(oList.BaseType);

                SetFieldVars(clientContext, oList, listForm.dgv_Data, ref fieldCount);
                SetViewVars(clientContext, oList, listForm.dgv_Data, ref defaultViewUrl, ref defaultViewTitle, ref viewCount);

                // Increment counter
                i++;

                //oList.DisableGridEditing = (listType == "List");
                //oList.Update();

                listForm.AddRow
                (
                    i.ToString(),
                    siteName,
                    siteUrl,
                    oList.Title,
                    oList.Description,
                    listType,
                    oList.EnableVersioning,
                    oList.MajorVersionLimit,
                    defaultViewTitle,
                    fieldCount,
                    viewCount,
                    itemCount,
                    !oList.DisableGridEditing,
                    oList.Id.ToString(),
                    oList.Created.ToString(),
                    defaultViewUrl,
                    oList.SchemaXml.ToString()
                );

                lbl_Row_Count.Text = i.ToString() + " record(s) found";
                lbl_Row_Count.Refresh();

                if (i >= nud_Row_Limit.Value && nud_Row_Limit.Value != 0)
                {
                    break;
                }
            }

            //clientContext.ExecuteQuery();
        }