views:

288

answers:

2

Sharepoint Controls require SPContext.current.site/web to work, but i am opening many sites using site= new spsite(siteID); and i want to use Controls. So do u have any idea or available class to use asp.net controls in sharepoint?

A: 

//open site and web sSiteID = Request.QueryString["siteID"]; sWebID = Request.QueryString["parentWebID"]; site = new SPSite(new Guid(sSiteID)); web = site.OpenWeb(new Guid(sWebID)); //show the properties of the list in the edit form (...) if ((list.AllowContentTypes == true) && (list.ContentTypesEnabled == true)) { (...)

                SharePointWebControls oSharePointWebControls = new SharePointWebControls(); 

       cntrl = oSharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Edit, "");

}}

public Control GetSharePointControls(SPField field, SPList list, SPListItem item, SPControlMode mode, string strType) { switch (field.FieldRenderingControl.ToString()) { case "Microsoft.SharePoint.WebControls.TextField": return CreateTextFieldControl(field, list, item, mode);

}

#region Create SharePoint Controls private static Control CreateTextFieldControl(SPField field, SPList list, SPListItem item, SPControlMode mode) { TextField tf = new TextField(); //tf.EnableViewState=false; tf.ListId = list.ID; if (item != null) { tf.ItemId = item.ID; } tf.FieldName = field.Title; tf.ID = "Field_" + field.Id; //tf.CssClass = "spsControl"; tf.ControlMode = mode; //check if the field has a default value if (field.DefaultValue != "null" && field.DefaultValue != null) { tf.Text = field.DefaultValue.ToString(); } try { RequiredFieldValidator cntrlValidator = ((RequiredFieldValidator)tf.Controls[0].Controls[3]); } catch (Exception ex) { }

        return tf;
    }

all sharepoint controls i am using are working and tf is returned correctly, but when i am not in the current site or the current web this exception occured : InvalidArgumentException in the control. I guess these controls does not work outside the current site or web, and i have to use asp.net control? Is it right or is there another solution? Thanks in advance...

Manale
can you format the code and move it to the question itself? You essentially need to fake the spcontext and load the other sitecollection to it.
Anirudh Goel
+1  A: 

Fake Spcontext is the solution for your problem. google it, it might help you.

Gaby
It did help. thx
Manale