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();

    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string test = _SampleText;
            string titles = Test(test);
            Textbox1.Text = titles;
        }
    }

    [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;


        }
    }




    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' />"
                                                );

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

        }

    }


}
  • There is no error in this but my value just cannot display in my web part after I click "Apply" – A_B_C May 15 at 8:01

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.