I'm trying to return all the child nodes of a set of navigation nodes in sharepoint, the SDK implies I should be doing something like this:
NodeColl = objSite.Navigation.TopNavigationBar
Dim Node as SPNavigationNode
For Each Node In NodeColl
if Node.IsVisible then
Response.Write("<siteMapNode url=""" & Node.Url & """ title=""" & Node.Title & """ description=""" & Node.Title & """ >" & Environment.NewLine)
Dim SubChildNodes as SPNavigationNodeCollection = Node.Children
Response.Write(SubChildNodes.Count) 'returns 0 always even though I know theres over 20 nodes in some of the sections
Dim ChildNode as SPNavigationNode
For Each ChildNode in SubChildNodes
if ChildNode.IsVisible then
Response.Write("<siteMapNode url=""" & ChildNode.Url & """ title=""" & ChildNode.Title & """ description=""" & ChildNode.Title & """ />" & Environment.NewLine)
End if
Next
Response.Write("</siteMapNode>" & Environment.NewLine)
End If
Next
however whenever I do, it lists the top level navigation nodes but I cannot get the children to be displayed.
I was facing the same issue: I was trying to access the SPWeb.Navigation.Quicklaunch from a web-scoped feature's feature receiver activated from onet.xml, but the SPWeb.Navigation.QuickLaunch.Count was always 0, even though I had definitely added list instances in other features activated previously in the same onet.xml.
The solution for me was to open a new SPSite and a new SPWeb in my feature receiver, after which I was able to access the quick launch items. For example this worked for me:
using (SPSite site = new SPSite("http://yourserver/"))
{
using (SPweb web = site.OpenWeb("theweb"))
{
web.Navigation.QuickLaunch.Count ; // greater than zero
// manipulate your quick launch here
}
}
I'm assuming this is because creating the new SPWeb object loads the web's most recent state from database, and the SPWeb passed to my feature receiver didn't represent the latest state. But this is me gues
I have this same problem, I found the solution as
using (SPSite site = new SPSite("http://server"))
{
using (SPWeb web = site.OpenWeb())
{
SPNavigationNode quicklaunch = web.Navigation.GetNodeById(1025);
if (quicklaunch != null)
{
foreach (SPNavigationNode heading in quicklaunch.Children)
{
PrintNode(heading);
}
}
}
}
static void PrintNode(SPNavigationNode node)
{
foreach (SPNavigationNode item in node.Children)
PrintNode(item);
}
Make sure you have a SiteMapDataSource associated with your master page quick launch.
Related
I am getting very strange error, worked out for last couple of hours to fix this
"Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb. "
Public Shared Sub DeleteListItem(ByVal listname As SPList, ByVal intItemID As Integer)
Using MySite As New SPSite(SPContext.GetContext(System.Web.HttpContext.Current).Web.Url)
Using MyWeb As SPWeb = MySite.OpenWeb()
MyWeb.AllowUnsafeUpdates = True
Dim itemColforGivenList As SPListItemCollection
Dim query As New SPQuery()
query.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" &
intItemID & "</Value></Eq></Where>"
MyWeb.AllowUnsafeUpdates = True
itemColforGivenList = listname.GetItems(query)
If itemColforGivenList.Count > 0 Then
For i As Integer = listname.Items.Count - 1 To 0 Step -1
If listname.Items(i).ID = intItemID Then
MyWeb.AllowUnsafeUpdates = True
listname.Items.Delete(i)
listname.Update()
MyWeb.AllowUnsafeUpdates = False
End If
Next
End If
End Using
End Using
Please help me out
Have you tried calling MyWeb.Update(); directly after MyWeb.AllowUnsafeUpdates = true; ? It's possible that the listname.Update() call is checking this property by pulling directly from the web and not the object representing that web you have in the current context. This does create a couple concerns as you will need to call update on that web/list multiple times to enable and disable the property and also delete the item so keep that in mind.
I've a SharePoint 2010 page with a list. The list has several items and a field named "Department" and must filter items based on user's department value retrieved from user profile.
To do this I've created a feature which upon activation adds UserContextFilterWebPart to the page and makes connection between UserContextFilterWebPart and XsltListViewWebPart. After the feature is activated I can see in the page design mode that connection is established but the list gets empty. Then I open web part's menu, choose "Connections" then "Send Filter Values To" and click "List1". When dialog appears I do nothing but only click "Finish" button and it begins to work fine. Can anybody please explain me why the connection begins to work only if I do that manual extra action? What must be done to fix?
I tried different way when List.Views[0].Query property is set to appropriate CAML query and it also works fine. But I'm told that it's not a good approach because of performance and parallel tasks issues. Is it really bad course of action?
Below is the code for 2 different approaches.
Thanks in advance!
1-s variant with UserContextFilterWebPart:
SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
UserContextFilterWebPart UCFWP = new UserContextFilterWebPart();
UCFWP.Title = "Current User Filter";
UCFWP.AllowEdit = true;
UCFWP.FilterName = "Current User";
UCFWP.SendEmptyWhenNoValues = true;
UCFWP.AllowClose = true;
UCFWP.ExportMode = WebPartExportMode.All;
UCFWP.AllowConnect = true;
UCFWP.AllowHide = true;
UCFWP.ProfilePropertyName = "Department";
UCFWP.ValueKind = UserContextFilterValueKind.ProfileValue;
UCFWP.ZoneID = "Main";
WPM.AddWebPart(UCFWP, UCFWP.ZoneID, 1);
WPM.SaveChanges(UCFWP);
ConsumerConnectionPointCollection consumerConnections = WPM.GetConsumerConnectionPoints(List);
ConsumerConnectionPoint addConsumerConnPoint = consumerConnections["DFWP Filter Consumer ID"];
ProviderConnectionPointCollection providerConnections = WPM.GetProviderConnectionPoints(UCFWP);
ProviderConnectionPoint addProviderConnPoint = providerConnections["ITransformableFilterValues"];
TransformableFilterValuesToParametersTransformer trans = new TransformableFilterValuesToParametersTransformer();
trans.ConsumerFieldNames = new string[] { "Department" };
trans.ProviderFieldNames = new string[] { "Department" };
SPWebPartConnection newConnection = WPM.SPConnectWebParts(UCFWP, addProviderConnPoint, List, addConsumerConnPoint, trans);
WPM.SPWebPartConnections.Add(newConnection);
2-nd variant with CAML query (intended to be used not in a feature but in a web part):
SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
SPUser CurrentUser = Web.CurrentUser;
SPServiceContext context = SPServiceContext.GetContext(Site);
UserProfileManager upm = new UserProfileManager(context, false);
UserProfile up = upm.GetUserProfile(CurrentUser.RawSid);
String UserDepartment = up["Department"].Value.ToString();
SPView ListView = Web.Lists["List1"].Views[0];
ListView.Query = "<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>" + UserDepartment + "</Value></Eq></Where>";
ListView.Update();
I had a similar problem of connecting two web parts. I found the answer here: http://kvdlinden.blogspot.dk/2011/02/programmatically-connect-two.html
Note that that post describes how to do it with two XsltListViewWebParts. In order to use it in your case I suggest that you:
Create the connection manually,
Use PowerShell to get a SPLimitedWebPartManager for the page,
Use the manager to iterate through the manager.SPWebPartConnections,
And find the ProviderConnectionPointID for your connection,
Use that ID in the code shown in the post.
Also remember to setup the transformer - you can find this also from the SPWebPartConnections.
Next time you activate your feature you should have a connection equal to the one you made by hand.
I have some data that's currently stored in an Excel workbook. It makes sense for the data to be in Excel (in that it's easy to manage, easy to extend, do calcs, etc.) but some of the data there is required by an automated process, so from that point of view it would be more convenient if it were in a database.
To give the information more visibility, workflow, etc. I'm thinking of moving it to SharePoint. Actually turning it into a SharePoint form would be tedious & time-consuming, and then the flexibility/convenience would be lost; instead, I'm thinking of simply storing the current Excel file within a SharePoint library.
My problem then would be: how can the automated process extract the values it needs from the Excel workbook that now lives within the SharePoint library? Is this something that Excel Services can be used for? Or is there another/better way? And even if it can be done, is it a sensible thing to do?
Having gone through something similar, I can tell you it actually isn't that bad getting values out of an Excel file in a document library. I ended up writing a custom workflow action (used within a SharePoint Designer workflow) that reads values out of the Excel file for processing. I ended up choosing NPOI to handle all of the Excel operations.
Using NPOI, you can do something like this:
// get the document in the document library
SPList myList = web.Lists[listGuid];
SPListItem myItem = myList.GetItemById(ListItem);
SPFile file = myItem.File;
using (Stream stream = file.OpenBinaryStream())
{
HSSFWorkbook workbook = new HSSFWorkbook(stream);
HSSFSheet sheet = workbook.GetSheet("Sheet1");
CellReference c = new CellReference("A1");
HSSFRow row = sheet.GetRow(c.Row);
HSSFCell cell = row.GetCell(c.Col);
string cellValue = cell.StringCellValue;
// etc...
}
You could easily put this in a console application as well.
Yes, I am trying to extract a range of cells on several sheets within a workbook. I was able to use some of the code below in a console application and view the data within the command window. Now I need to dump the data to a SQL Table and was looking for some examples on how to accomplish this and make sure I am going down the correct coding path.
Here is a snapshot of the code I am using.
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.RootWeb)
{
SPList docList = web.Lists[__ListId];
SPListItem docItem = docList.GetItemById(__ListItem);
SPFile docFile = docItem.File;
using (Stream stream = docFile.OpenBinaryStream())
{
HSSFWorkbook wb = new HSSFWorkbook(stream);
//loop through each sheet in file, ignoring the first sheet
for (int i = 1; i < 0; i++)
{
NPOI.SS.UserModel.Name name = wb.GetNameAt(i);
String sheet = wb.GetSheetName(i);
NPOI.SS.UserModel.Name nameRange = wb.CreateName();
nameRange.NameName = ("DispatchCells");
//start at a specific area on the sheet
nameRange.RefersToFormula = (sheet + "!$A$11:$AZ$100");
}
wb.Write(stream);
}
}
}
return ActivityExecutionStatus.Closed;
}
I would like to programatically add alert to folders inside a sharepoint list. I have found how to set alerts to a list and this works perfect.
Could someone please help me on how to set alerts to a specific folder which is inside a list.
Below is the code i currently have which sets alerts only to the list.
using (SPSite site = new SPSite("http://site/"))
{
using (SPWeb web = site.OpenWeb())
{
SPUser user = web.SiteUsers["domain\\user"];
SPAlert newAlert = user.Alerts.Add();
newAlert.AlertType = SPAlertType.List;
newAlert.List = web.Lists["Documents"];
newAlert.EventType = SPEventType.All;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
//passing true to Update method will send alert confirmation mail
newAlert.Update(true);
}
}
Your help will be much appreciated
THIS QUESTION IS RESOLVED! PLEASE SEE MY POST BELOW WITH THE LINK - SEE - LINK
That's not possible out of the box, but after googling I found an interesting possibility though, check out Mike Walsh's answer on this post, it entails creating a view in the folder and then attaching the alert to that view.
You need to update the line with
newAlert.List = web.Lists["Documents"];
With
SPFolder fldr = web.GetFolder("/ListName/FolderName");
newAlert.Item=fldr.Item;
Also note that Folder is also another item.
I am forced to use SharePoint web services. I need a web service which will let me delete the SharePoint sub site. I have tried to use DeleteWorkspace method (Meetings.asmx web service), but it is capable of only deleting the sub sites that are meeting workspaces (mine uses a custom template derived from team site). Any help would be appreciated, thanks.
Amazingly! No you can't do it.... I know! weird that it would left out. I'm sure there was a decision made somehwere about but beats me if I know why.
The only option is to deploy custom code - either an event receiver or a web service.
Unfortunately this isn't possible with the out-of-the-box web services. (They only have functionality for deletion at the site collection level.)
You would need to develop a custom web service and deploy that to your SharePoint farm.
If you upgrade to SharePoint 2013, there is a new method in the Sites web service: DeleteWeb. It expects URL of the subsite to delete, relative to the site that you connected the web service to.
--- Ferda
like Ben Says, using /_vti_bin/Dws.asmx should be works. Here's another example
public bool DeleteSubSite(string urlSubSite, string user, string passw, string domain)
{
bool retValue = true;
Dws docWS = new Dws();
docWS.Url = urlSubSite + "/_vti_bin/Dws.asmx"; ;
docWS.Credentials = new System.Net.NetworkCredential(user, passw, domain);
try
{
docWS.DeleteDws();
}
catch (SoapException soex)
{
retValue = false;
}
return retValue;
}
If you want to delete a site try using the dws webservice.
I used DWS.DeleteDWS() where the functoins get_constant etc. simple get back constants for login and webservices like _vti_bin/dws.asmx
Public Function RemoveWSSSite(ByVal sPath As String, ByVal sSubSiteName As String) As Boolean
Dim DTConstant As New DTFrameWork.DTConstant
Dim SPDWS1 As New SPDws.Dws
Dim sSubsiteURL As String = ""
If (sSubSiteName = "") Then
sSubsiteURL = ""
Else
sSubsiteURL = sSubSiteName & "/"
End If
SPDWS1.PreAuthenticate = True
SPDWS1.Credentials = New System.Net.NetworkCredential(DTconst.Get_Constant_String_Value("SP_m_AdminUser"), DTconst.Get_Constant_String_Value("SP_m_AdminPassword"), DTconst.Get_Constant_String_Value("SP_m_SiteDomain"))
SPDWS1.Url = DTconst.Get_Constant_String_Value("SP_m_SiteServerName") & IIf(sPath.StartsWith("/"), "", "/") & sPath & IIf(sPath.EndsWith("/"), "", "/") & sSubsiteURL & DTconst.Get_Constant_String_Value("SP_m_dws_asmx")
Try
SPDWS1.DeleteDws()
Return True
Catch ex As Exception
Return False
End Try
End Function