I have a problem with a site definition, it has several site pages, in those pages i need to have a top menu, so i created a user control, that draw that menu, and added it to the pages.
After deploying the solution i can create the site just fine.
In the initial phase, i have hard coded values for the menu links, and in each of the pages i could see the menu.
Now the menu it's dynamic, so i have a sharepoint list with the menu options, so i changed the code, instead of hard-coded values, i get them from a list, the code runs fine, i can check that while debugging, but in the end i get a complete blank page.
I can't interact with sharepoint data inside a user control that will be used in a site page?
I tested the control in a normal/application page and it works just fine, in both cases (hard-coded and sharepoint data).
A simple sample of the code:
protected void Page_Load(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToLongTimeString();
ddlData.Items.Clear();
//Option Manual - WORKS FINE
ddlData.Items.Add("Manual 1");
ddlData.Items.Add("Manual 2");
ddlData.Items.Add("Manual 3");
//Option Sharepoint Data - DOESNT WORK
SPSecurity.RunWithElevatedPrivileges(delegate () {
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.RootWeb)
{
foreach (SPList lista in web.Lists)
{
ddlData.Items.Add(lista.Title);
}
}
}
});
}
The rest of the code, it's a simple page with the reference to this user control, and the aspx code of the user control only have the declaration of this two controls.
After several tests, i came to conclusion that i have two problems:
1 - In this project for some reason, unknown to me, inside the code someone had added some code to clear the errors, that's why i had the blank page.
2 - After seeing the error, i could confirm that if i use site pages and trying to get the data with Using, when it tries to dispose the objects it throws the exception, so the solution was simple, just remove the Usings.
Thanks
Related
a bit of an iOS beginner here. I am tasked with creating a button that will navigate to the previous view controller. I have tried a lot of different ways, but the most recent is as follows:
partial void ButtonPressedPreviousOne(UIButton sender)
{
UIStoryboard HomeStoryboard = UIStoryboard.FromName("Home", null);
UserProfileViewController prevController = HomeStoryboard.InstantiateViewController("User_Profile_VC") as UserProfileViewController;
NavigationController.PopToViewController(prevController, true);
}
This throws a null reference exception. I only partially understand what is meant by this. Is there something wrong with the above code or am I missing something entirely different?
iOS Navigation controller has method NavigationController.PopViewController(true).
Try this one. But I strongly recommended check stack before do this like
public bool CanGoBack => NavigationController.ViewControllers.Length > 1;
Let see how navigation works:
Push:
To move from one page to another, an application will push a new page
onto the navigation stack, where it will become the active page, as
shown in the following diagram:
When we push a page, we usually use:
NavigationController.PushViewController();
Pop:
To return back to the previous page, the application will pop the
current page from the navigation stack, and the new topmost page
becomes the active page, as shown in the following diagram:
When we pop a page:
If you want to go back to previous page, use:
NavigationController.PopViewController(true);
If you want to go back to root page, use:
NavigationController.PopToRootViewController(true);
If you want to go back to specific page in Navigation Stack, use:
NavigationController.PopToViewController(viewController, true);
Refer : hierarchical Navigation
Back to your code:
If you use PopToViewController, the first paramater(prevController in your code) should be a existing ViewController in Navigation Stack instead of a new created. You got a null reference exception because the prevController you created is not in your Navigation Stack.
In your case, you can use:
var navController = this.NavigationController;
foreach (var controller in navController.ViewControllers)
{
if (controller is UserProfileViewController)
{
NavigationController.PopToViewController(controller,true);
break;
}
}
Note: if UserProfileViewController is the previous page you want to pop, use NavigationController.PopViewController(true); directly is simpler and faster.
I'm using a browser-enabled Infopath form with Sharepoint 2010 and I want to show a modal popup or even redirect to another page after clicking on Submit, saying "Thank you... Your form has been sent...".
I can't use a View (and change to that view on submit) because the form is inside a 'tab' in the page. I mean, the page contains 5 DIVs acessible through 5 buttons.
The DIV which is open by default is the 1st one. The form is inside the 5th DIV.
After the submit action (postback) the page reloads and the 1st DIV is shown, so the user can't see the View with "Thank you...."
Any ideias on how to solve this?
Thanks in advance!
EDIT 2
I tried Andreas' solution but didn't work.
This is my "submit" button code.
public void CTRL114_5_Clicked(object sender, ClickedEventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
this.Submit();
HttpContext.Current.Response.Redirect("ThankYou.aspx");
});
} catch (Exception ex)
{
System.Console.Write(ex.toString());
}
}
When I click on Submit, nothing happens. No page redirection, no view switching, no data submitting.
Any ideas?
Thank you!
Popup's do not work with browser-enabled forms, your only possibility will be custom code. In your submit code, you can do a redirect to a static thank you page.
HttpContext.Current.Response.Redirect("ThankYou.aspx");
Edit: In your VSTA Editor, make sure you have System.Web referenced. Without an actual exception it's really hard to say what's not working. Try the following code and post the output please -
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
this.Submit();
HttpContext.Current.Response.Redirect("ThankYou.aspx");
});
}
catch(Exception ex)
{
//implement your logging logic here
}
Edit 2: Can you open the form in preview mode and debug the code? Also consider changing your logging code to something like this:
catch(Exception ex)
{
using(var writer = new StreamWriter(#"C:\log.txt",true))
writer.WriteLine("{0}\t{1}", DateTime.Now, ex.Message);
}
because Console.WriteLine does not work with InfoPath and you never get hold of the actual exception. I'm afraid without a error messsage it will be quite hard to troubleshoot your issue any further.
Another way of redirecting is using the 'Source' parameter of the query string. It is, however, only possible to redirect to pages in the same site collection as the form. E.g.
http://[site]/_layouts/FormServer.aspx?XsnLocation=/sites/[site]/formservertemplates/[form].xsn<b>&Source=/sites/[site]/pages/[page]</b>
If ThankYou.aspx is outside the current site colleciton, you can redirect to a intermediary page which redirects to ThankYou.aspx. I have heard of no other way to bypass this.
I'm trying to make a site map for a Sharepoint 2010 that will list every site and that site's current nav items. Navigation between sites is done via the top nav. However, I'm having problems getting the current nav items for each site. It appears that each time I get the PortalSiteMapProvider for the web, it returns the provider for the whole site collection. How do I get the items for just that site? I run this code for each site I want to get the items for:
var provider = new PortalSiteMapProvider();
provider.NavigationType = PortalNavigationType.Current;
provider.CurrentWeb = web;
var rootNode = provider.RootNode;
Anyone have any idea where to go from here?
EDIT:
So it isn't possible to just access each site collection and run this code. The PortalSiteMapProvider doesn't allow it. It is, however, possible to run this code in a ashx that's located in the layouts folder. I just need to access that ashx file from the site collection I want to enumerate, and it will work just fine :)
private void DrawWeb(SPWeb web, TreeNode node)
{
SPWebCollection webCol = web.Webs;
foreach (SPWeb w in webCol)
{
var n = new TreeNode(w.Title);
node.ChildNodes.Add(n);
DrawWeb(w, n);
w.Dispose();
}
}
Try calling this method like:
TreeNode webNode = new TreeNode(rootNode.Title);
DrawWeb(provider.CurrentWeb, webNode);
Hope this will be helpful.
So it isn't possible to just access each site collection and run this code. The PortalSiteMapProvider doesn't allow it. It is, however, possible to run this code in a ashx that's located in the layouts folder. I just need to access that ashx file from the site collection I want to enumerate, and it will work just fine :)
I have not worked with webparts for sharepoint before, but need to make change to a webpart, that needs to be propagated to some 700 websites. It is a change to one of the properties of the webpart, the value needs to be changed. Is there a way to get metadata for a webpart and change it directly in the database (I assume that is where it is stored.)?
Here is the scenario: Webpart contains a comma delimited list of document types (internal type) that it should display. Now there are new doc. types that need to be added to all 700 websites. I need a way to enumerate websites, get the webpart metadata, and add these new doc types to webpart. Currently they go manually to each website, click on edit, type in new doc type, and save it.
As others have said the correct approach is to programmatically achieve this rather than edit the content database which will make your installation unsupportable. I regularly use a console application to do this in a site collection made up of sites created from a site template.
Here is an example that changes the Title property of a ListViewWebPart. Updated to include code for recursive loop. I haven't tested this but it should work.
private static void ProcessSiteCollection(string url)
{
using (SPSite siteCollection = new SPSite(url))
{
SPWeb rootWeb = siteCollection.RootWeb;
ProcessWebs(rootWeb);
}
}
private static void ProcessWebs(SPWeb parentWeb)
{
foreach (SPWeb web in parentWeb.Webs)
{
try
{
UpdateWebPart(web); // Set web part properties
ProcessWebs(web); // Recursively loop through children
}
finally
{
web.Dispose();
}
}
}
private static void UpdateWebPart(SPWeb web)
{
using (SPLimitedWebPartManager webPartManager =
web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if (webPart.Title == "My Web Part")
{
ListViewWebPart listViewWebPart = (ListViewWebPart)webPart;
listViewWebPart.Title = "Updated Web Part";
webPartManager.SaveChanges(listViewWebPart);
web.Update();
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
}
Directly accessing the sharepoint content databases is a big "no no." That's the official answer. :)
That being said, I have only ever looked in the content databases and never tried to actually change anything manually.
My suggestion, would be to modify the existing web part to modify the property based on currently set property(s). (I am assuming that some currently set property is invalid or needs to be updated based on changes to the infrastructure.) ... If this is the case, you can validate the property; making sure that current property is changed to what it needs to be, and/or making sure future property changes are valid.
Good luck!
DON'T
Seriously, do not go into the content databases and edit it. That way you are not supported anymore if anything should happen and Microsoft will not support you anymore (not until you revert the database back to an earlier version from a backup that is).
You can use the API to access webparts in your sites, here's some code that should get you started:
Enumerate page webparts
I want to use the MultipleLookupField control in a web page that will run in the context of SharePoint. I was wondering if anyone would help me with an example, which shows step by step how to use the control two display two SPField Collections.
I'm not entirely sure I understand your question, especially the bit about displaying two SPField collections. Sorry if this turns out to be the answer to a completely different question!
Anyway here's a quick demo walkthrough of using the MultipleLookupField in a web part.
Create a team site. Add a few tasks to the task list. Also put a document in the Shared Documents library. Create a new column in the Shared Documents library; call it "Related", have it be a Lookup into the Title field of the Tasks list, and allow multiple values.
Now create a web part, do all the usual boilerplate and then add this:
Label l;
MultipleLookupField mlf;
protected override void CreateChildControls()
{
base.CreateChildControls();
SPList list = SPContext.Current.Web.Lists["Shared Documents"];
if (list != null && list.Items.Count > 0)
{
LiteralControl lit = new LiteralControl("Associate tasks to " +
list.Items[0].Name);
this.Controls.Add(lit);
mlf = new MultipleLookupField();
mlf.ControlMode = SPControlMode.Edit;
mlf.FieldName = "Related";
mlf.ItemId = list.Items[0].ID;
mlf.ListId = list.ID;
mlf.ID = "Related";
this.Controls.Add(mlf);
Button b = new Button();
b.Text = "Change";
b.Click += new EventHandler(bClick);
this.Controls.Add(b);
l = new Label();
this.Controls.Add(l);
}
}
void bClick(object sender, EventArgs e)
{
l.Text = "";
foreach (SPFieldLookupValue val in (SPFieldLookupValueCollection)mlf.Value)
{
l.Text += val.LookupValue.ToString() + " ";
}
SPListItem listitem = mlf.List.Items[0];
listitem["Related"] = mlf.Value;
listitem.Update();
mlf.Value = listitem["Related"];
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
}
Granted, this is borderline ridiculous -- everything is hard-coded, there is no error-handling at all, and it serves no useful purpose -- but it's only meant as a quick demo. Now build and deploy this web part and add an instance of it to your team site's homepage; it should allow you to get and set the tasks which are associated with the first document in the library.
The strange bit towards the end of the button Click handler, where we read a value from mlf.Value and then write it back again, appears to be required if you want the UI to stay in sync with the actual list values. Try omitting the last line of bClick to see what I mean. This has been driving me nuts for the last hour or so, and I'm hoping another commenter can come up with a better approach...
Hm. Works fine on mine, so let's see if we can work out how your setup is different...
It looks as though it's having trouble populating the control; my first guess would be that this is because the code makes so many assumptions about the lists it's talking to. Can you check that you've got a plain vanilla Team site, with (assume these names are case-sensitive):
A list called Tasks, with several items in it
A library called Shared Documents with at least one document
A column called Related in the Shared Documents library
The Related column is a Lookup field into the Title column of Tasks, and allows multiple values.
The first document in Shared Documents has a value for Related
Then add the webpart. Fingers crossed...
Hm. OK, I'm still trying to break mine... so I went to the layouts directory and created a file foo.aspx. Here it is:
<%# Page Language="C#" Inherits="System.Web.UI.Page" MasterPageFile="~/_layouts/simple.master" %>
<%# Register Tagprefix="foo" Namespace="Foople" Assembly="Foople, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"%>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<foo:WebPart1 id="fred" runat="server" />
<foo:WebPart1a id="barney" runat="server" />
</asp:Content>
WebPart1 is the webpart from before. WebPart1a is the exact same code, but in a class that inherits directly from WebControl rather than from WebPart.
It works fine, apart from a security validation problem on the postback that I can't be bothered to debug.
Changing the masterpage to ~masterurl/default.master, I uploaded foo.aspx to the Shared Documents library, and it works fine from there too -- both the WebControl and the WebPart behave properly, and the security problem is gone too.
So I'm at a loss. Although I did notice this page with an obscure might-be-bug which is also in SPFolder.get_ContentTypeOrder(): http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/63baf273-7f36-453e-8293-26417759e2e1/
Any chance you could post your code?