I'm create a timer job in sharepoint and have made it quite far. I am running into the issue of having a "this" object in a static method. Here is my code. If anyone has any suggestions, that would be great.
public static SPListItemCollection GetRecordwithMissingData (string DocType, string DocName)
{
//Access associate ID and Doc Name
//SPWeb web = properties.Feature.Parent as SPWeb;
SPWebApplication webapp = this.Parent as SPWebApplication;
SPSite hrdocsSite = webapp.Sites["sites/HRDOCS/HRDocuments"];
SPWeb rootweb = hrdocsSite.RootWeb;
SPList AssociateDocumentsList = rootweb.Lists["Associate Documents"];
SPListItemCollection AssociateDocuments = AssociateDocumentsList.GetItems("ID", "PrimaryName", "DocName", "Document Type");
// stores Associate Documents ^
You cannot have "this" in a static method. There is no instance of what "this" is because it refers to the current instance. One way to fix the problem is to pass an extra parameter to pass an instance of the object you are trying to access the property from. The other option is to have a static instance of whatever type "this" is in the class so the method can access it.
Related
We are not able to hide the SET area that is displayed in top navigation bar, I am using the below code snippet to achieve the same. But the subsite is not getting hidden even when the code is not throwing any error. Bit clueless as after unsafe update the code is functioning as expected.
Code Snippet:
using (SPSite siteCollection = new SPSite("http://****:****/VijaiTest/"))
{
using (SPWeb web = siteCollection.RootWeb)
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
// Global Navigation
//Show Subsites
publishingWeb.Navigation.GlobalIncludeSubSites = false;
//Show Pages
publishingWeb.Navigation.GlobalIncludePages = false;
// Maximum number of dynamic items to show within this level of navigation:
publishingWeb.Navigation.GlobalDynamicChildLimit = 60;
publishingWeb.IncludeInCurrentNavigation = false;
web.AllowUnsafeUpdates = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Update the changes
publishingWeb.Update();
});
}
}
I see a few potential problems with your code...
1. Don't wrap SPWeb web = sitecollection.RootWeb in a Using statement
While normally it's good practice to wrap SPSite and SPWeb objects in Using statements to ensure that they're disposed of properly, the SPSite.RootWeb property is an exception to this rule. The root web is disposed automatically along with the SPSite object, when it is disposed. Since you have SPSite siteCollection = new SPSite(... wrapped in a Using statement, you don't need to worry about disposal of the RootWeb.
Trying to dispose of the root web twice will add errors to your logs and can cause problems when accessing that web object programmatically.
2. Instantiate your SPSite and SPWeb objects inside your SPSecurity.RunWithElevatedPrivileges delegate
For SPSecurity.RunWithElevatedPrivileges to be effective, you must retrieve or create your SPSite and SPWeb objects within the delegate function.
Your code obtains the SPSite and SPWeb objects prior to running RunWithElevatedPrivileges, so any operations on those objects will run under the context of the current user instead of running with elevated privileges.
3. Check to be sure the SPWeb object is a valid PublishingWeb before executing GetPublishingWeb(web)
From Microsoft:
Before you use this method, check the IsPublishingWeb method to confirm that publishing behavior is supported on this instance of the SPWeb class. If publishing is not supported on the SPWeb, then the methods and properties of the PublishingWeb wrapper may behave unexpectedly.
After those changes, your code would look like this:
SPSecurity.RunWithElevatedPrivileges(delegate() {
using(SPSite siteCollection = new SPSite("http://****:****/VijaiTest/")) {
SPWeb web = siteCollection.RootWeb;
if(PublishingWeb.IsPublishingWeb(web)){
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
// Don't show Subsites
publishingWeb.Navigation.GlobalIncludeSubSites = false;
// Don't show Pages
publishingWeb.Navigation.GlobalIncludePages = false;
// Maximum number of dynamic items to show within this level of navigation:
publishingWeb.Navigation.GlobalDynamicChildLimit = 60;
publishingWeb.IncludeInCurrentNavigation = false;
web.AllowUnsafeUpdates = true;
//Update the changes
publishingWeb.Update();
}else{
throw new Exception("Web is not a publishing web");
}
}
});
I have the following activation code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Create a new list and populate it.
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
web.Lists.Add("Projects", "Projects That are currently being worked on.", SPListTemplateType.GenericList);
web.Update();
// Add the new list and the new content.
SPList projectList = web.Lists["Projects"];
projectList.Fields.Add("Name", SPFieldType.Text, false);
projectList.Fields.Add("Description", SPFieldType.Text, false);
projectList.Update();
//Create the view? - Possibly remove me.
System.Collections.Specialized.StringCollection stringCollection =
new System.Collections.Specialized.StringCollection();
stringCollection.Add("Name");
stringCollection.Add("Description");
//Add the list.
projectList.Views.Add("Project Summary", stringCollection, #"", 100,
true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
projectList.Update();
}
}
Which should go through and add a new list called project and its associated view. How ever when running the app I get:
'Activate Features': Object reference not set to an instance of an
object
My questions are:
Why is this happening? The activation happens at a site level. and I am the admin of the "development" site.
Should I be checking each time to make sure this list doesn't already exist? (each time, referring to each time I hit deploy)
I'm going to assume that you have a Site scoped feature and that your NullReferenceException is being caused by you attempting to cast properties.Feature.Parent as SPWeb.
If my assumption about your feature being Site scoped is correct you can't get access to an SPWeb the way you are trying. Try this instead:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null)
{
SPWeb web = siteCollection.RootWeb;
// Rest of your code here.
}
}
EDIT: I think I can simplify this question a bit to ask for only what is needed to know:
I am working with C# using the SSRS 2010 Web Service:
'ReportService2010.asmx' http://technet.microsoft.com/en-us/library/ee640743.aspx
I can use the method 'CreateDataSource' to create a Datasource on an instance of an SSRS Server http:// (servername)/ReportServer.
I can also use the method 'CreateCatalogItem' to create a report on a server from referencing a project's RDL local file to serialize it to a byte array and then pass that as a 'Definition' to the method to create it on the server.
Now everything I do works with a caveat, and a major one. I can only deploy everything to the same folder. If I deploy a Data Source to say the 'Data Sources' folder and then a report to say: 'Test Reports', the report does not know it has a shared data source to reference at a different location. So I dug a little at the technet articles and have tried to 'GetItemDataSources' method but it only gives a name and a type for the ReportingService2010.DataSource return type. Does anyone know the method to link up a 'Report' or 'Dataset's CatalogItem property of 'DataSource', so it points to a reference in a different folder on the SSRS Server when deploying? There has to be a way to do it as I know I can deploy from Business Intelligence Development Studio and it can do this.
I've had similar issues when deploying report files; when deploying through rs.exe or code you run into these issues where reports lose their link to a Data Source.
We solved this by explicitly pointing the report to the server-side Data Source immediately after being deployed by our application; is this similar to what you're trying to do?
Anyway, here's the slightly adapted code we use in our report deployment application:
static void SetReportDataSource(string reportPath)
{
string dsPath = CombinePath(DataSourcePath, DataSourceFolder, DataSourceName);
DataSourceReference dsRef = new DataSourceReference()
{
Reference = dsPath
};
DataSource ds = new DataSource();
ds.Item = dsRef as DataSourceDefinitionOrReference;
ds.Name = DataSourceName;
var rptDataSources = Server.GetItemDataSources(reportPath);
foreach (var rptDs in rptDataSources)
{
Server.SetItemDataSources(filePath, new DataSource[] { ds });
}
}
So, basically we have variables that define information like the Data Source name, Data Source location on server, and the same for a report. They can be in different folders.
Based on this, we create a new reference to a Data Source and then repoint the report to this using SetItemDataSources.
This sorted out the Data Source issue for me, anyway. Not sure about Shared Datasets and how they handle all of this, but hopefully this will be of some help.
Also, just thought that this would be using the ReportService2005 endpoint, but it's probably not too different for ReportService2010.
Edit:
For the paths mentioned here, these are relative to the server, e.g. /Reports/. You don't need the fully qualified name as you define the Url property of the ReportService2010 object which contains the destination.
Maybe this might be some help. I used it to reset the DataSource for all the reports in a given Parent Folder, and it's subfolders:
using System;
using GetPropertiesSample.ReportService2010;
using System.Diagnostics;
using System.Collections.Generic; //<== required for LISTS
using System.Reflection;
namespace GetPropertiesSample
{
class Program
{
static void Main(string[] args)
{
GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource("0_Contacts"); //<=== This is the parent folder
}
private static void GetListOfObjectsInGivenFolder_and_ResetTheReportDataSource(string sParentFolder)
{
// Create a Web service proxy object and set credentials
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
CatalogItem[] reportList = rs.ListChildren(#"/" + sParentFolder, true);
int iCounter = 0;
foreach (CatalogItem item in reportList)
{
iCounter += 1;
Debug.Print(iCounter.ToString() + "]#########################################");
if (item.TypeName == "Report")
{
Debug.Print("Report: " + item.Name);
ResetTheDataSource_for_a_Report(item.Path, "/DataSources/Shared_New"); //<=== This is the DataSource that I want them to use
}
}
}
private static void ResetTheDataSource_for_a_Report(string sPathAndFileNameOfTheReport, string sPathAndFileNameForDataSource)
{
//from: http://stackoverflow.com/questions/13144604/ssrs-reportingservice2010-change-embedded-datasource-to-shared-datasource
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string reportPathAndName = sPathAndFileNameOfTheReport;
//example of sPathAndFileNameOfTheReport "/0_Contacts/207_Practices_County_CareManager_Role_ContactInfo";
List<ReportService2010.ItemReference> itemRefs = new List<ReportService2010.ItemReference>();
ReportService2010.DataSource[] itemDataSources = rs.GetItemDataSources(reportPathAndName);
foreach (ReportService2010.DataSource itemDataSource in itemDataSources)
{
ReportService2010.ItemReference itemRef = new ReportService2010.ItemReference();
itemRef.Name = itemDataSource.Name;
//example of DataSource i.e. 'itemRef.Reference': "/DataSources/SharedDataSource_DB2_CRM";
itemRef.Reference = sPathAndFileNameForDataSource;
itemRefs.Add(itemRef);
}
rs.SetItemReferences(reportPathAndName, itemRefs.ToArray());
}
}
}
right now, I'm using the SPSecurity.RunWithElevatedPrivileges method to let anonymous users add list items to a list. What i would like to do is make a general method that takes a Site, List and List item as an argument and adds the item to the list being passed. Right now I have :
public static void AddItemElevated(Guid siteID, SPListItem item, SPList list)
{
SPSite mySite = SPContext.Current.Site;
SPList myList = WPToolKit.GetSPList(mySite, listPath);
SPWeb myWeb = myList.ParentWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite eleSite = new SPSite(mySite.ID))
{
using (SPWeb eleWeb = eleSite.OpenWeb(myWeb.ID))
{
eleWeb.AllowUnsafeUpdates = true;
SPList eleList = eleWeb.Lists[myList.Title];
SPListItem itemToAdd = list.Items.Add();
itemToAdd = item;
itemToAdd.Update();
eleWeb.AllowUnsafeUpdates = false;
}
}
});
}
The problem is that 'item' gets initialized outside of the elevated privileges so when 'itemToAdd' is set to 'item' it loses its elevated privileges, causing the code to break at 'item.update()' if used my an non-privileged user.
Any Thoughts?
The problem could be because you are passing in your list. Try just passing in the list name and then grabbing the list from the elevated web like this:
public static void AddItemElevated(SPListItem itemToAdd, string listName)
{
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(web.Url))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
{
elevatedWeb.AllowUnsafeUpdates = true;
SPList list = elevatedWeb.Lists[listName];
SPListItem item = list.Items.Add();
item = itemToAdd;
item.Update();
elevatedWeb.AllowUnsafeUpdates = false;
}
}
}
}
Following line itemToAdd = item; does something strange - you adding item to one list (with list.Items.Add() ) but updating item from another list/location (one that comes as argument).
Not sure what you actually want, but maybe you want to co copy all fileds from item to itemToAdd. Consider in this case to pass fieldName/value pairs as argument to make it clear that you are adding new item with given values.
Note, that anonymous users are able to add items to lists that explicitly allow it.
I haven't tried it but possibly this could help - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx
Regards,
Nitin Rastogi
If item is coming from an SPList.AddItem() method, the splist instance must be get from an elevated web. otherwise this code will always break for anonymous users.
or you can allow anonymous user to add item to list, so you won't need running the code with elevated privileges.
by the way, itemToAdd = item; is not a correct way of setting the new added item to an old instance.
I have a main form that is launched and then it can go to any of the other forms I have created. But the kicker is that I have written a class that I call that returns a string with the name of the form to go to.
Currently I don't have this working so I am going from form to form like this (statically written linking code):
this.Hide();
CloudAccess nextForm1 = new CloudAccess();
//Where CloudAccess is the class of the next form.
nextForm1.ShowDialog();
What I want is something like this:
FormController pick = new FormController();
//Where FormController is the Class I create an object of and ask what's next
string next = pick.whereToGo(); //lets say it returns "CloudAccess"
this.Hide();
next nextForm1 = new next(); //next is desired to be the contents of the string
nextForm1.ShowDialog();
The problem is that I don't know how to use the returned string to make the new object and use it. I've been looking at Invoke and Reflection topics like this one: Use string value to create new instance
But I'm new to C# and I'm not sure how to apply that to this scenario.
Thoughts? Thanks!
Here's the working code from what fejejosco said:
string asdf = "CloudAccess";
Type CAType = Type.GetType("namespace." + asdf);
Form nextForm2 = (Form)Activator.CreateInstance(CAType);
nextForm2.ShowDialog();
Thanks!
Get the type of the form: Type.GetType("name.space." + formname), so if your class is name.space.CloudAccessForm, then pass in CloudAccessForm as formname, and it will give you the type. Then you can instantiate it with Activator.CreateInstance(type). Then cast it to a Form, and show it.