0

I have created a web part property to take in a string value by user and then search my document library for a list of documents that contains the specific value entered, for example "ABC". However, whenever i key in "ABC" and press "Apply", an error returns. Here is my full code from visual studio. Thanks for the help guys, appreciate it :)

private string abc; private TextBox Textbox1; public static string SampleText; private Label errorMessage = new Label(); private string titles;

    protected override void CreateChildControls()
    {
        string fourthterm = _SampleText;
        string results = Test(fourthterm);



        try
        {
            Textbox1.Text = results;
            Textbox1.DataBind();
        }
        catch (Exception ex)
        {
            errorMessage.Text += ex.Message;
        }
        // Add control to the controls collection of the Web Part.
        Controls.Add(Textbox1);
        Controls.Add(errorMessage);
        base.CreateChildControls();

    }



    [Category("Filtering documents"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Number of documents"),
    WebDescription("Please Enter a Sample Text")]
    public string _SampleText
    {
        get { return SampleText; }
        set
        {

            // Sample Validation
            Regex oRegEx = new Regex("[a-zA-Z]+");
            if (!oRegEx.IsMatch(value))
                throw new Microsoft.SharePoint.WebPartPages.
                    WebPartPageUserException(
                    "Please enter alphabetic characters only");

            SampleText = value;


        }
    }

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    if (!Page.IsPostBack)
    //    {

    //        Textbox1.Text = this._SampleText;
    //    }
    //}


    public static string Test(string newinput)
    {
        //TASK 7
        using (SPSite site = new SPSite("http://teamsite.forest.dc/"))
        {
            string title = "";

            using (SPWeb web = site.OpenWeb())
            {
                // Build a query.
                SPQuery query = new SPQuery();
                query.Query = string.Concat(
                               "<Where><Eq>",
                                  "<FieldRef Name='Tags'/>",
                                  "<Value Type='TaxonomyFieldType'>"  + newinput + "</Value>",
                               "</Eq></Where>",
                               "<OrderBy>",
                                  "<FieldRef Name='Doc Name' Ascending='TRUE' />",
                               "</OrderBy>");

                query.ViewFields = string.Concat(
                                    "<FieldRef Name='Tags' />",
                                    "<FieldRef Name='Doc Name' />"
                       [![enter image description here][1]][1]                         );

                query.ViewFieldsOnly = true; // Fetch only the data that i need.

                SPList documentLibrary = web.Lists.TryGetList("TestLibrary3");
                SPListItemCollection items = documentLibrary.GetItems(query);


                foreach (SPListItem item in items)
                {

                    title += item["Title"].ToString();
                    title += ",";
                }
                return title;
            }

        }

    }
  • What is the error? – Nils May 15 at 7:43
  • can you add screenshot of error? I tried your exact code with test method and I am able to click on apply, save page and my values configured in webpart properties are displayed on textbox1.text – Siddharth Vaghasia May 15 at 7:44
  • Ok sir i will create another thread. My bad – A_B_C May 15 at 7:51
  • Hi sir, can u head to my new thread and see what's wrong? Thanks :) – A_B_C May 16 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.