Tuesday, 5 February 2013
For Those Wishing to Enable / Disable / Set Emails for Site Access Requests in an entire Site Collection
might I recommend this script from Michael Allen, which does the job with aplomb!
It's Always OK To Refuse To Entertain Trolls
A brief pause from the usual outpourings of code and blather to talk about a serious topic.
A few people in an online SharePoint comm to which I was recently invited expressed mild disagreement with my last post. Probably because it is safer to use JQuery and easier to maintain for administrators. The post merits debate and criticism, I agree. It could have been an opportunity for discussion. But.
One person trolled me. He was negative, provocative and told me I did not know what I was talking about. I told him that I would not entertain responses that were uncivil, as his was. After all, I didn't know this man from Adam and he had never responded to any of my posts before (presumably I had not yet done anything to give him excuse to find fault, so affording him little pleasure.)
In response I got a big pile of rage. The gist of same being that I needed to learn humility and take criticism.
One tiny problem: I cannot take criticism where none has been given.
A few people in an online SharePoint comm to which I was recently invited expressed mild disagreement with my last post. Probably because it is safer to use JQuery and easier to maintain for administrators. The post merits debate and criticism, I agree. It could have been an opportunity for discussion. But.
One person trolled me. He was negative, provocative and told me I did not know what I was talking about. I told him that I would not entertain responses that were uncivil, as his was. After all, I didn't know this man from Adam and he had never responded to any of my posts before (presumably I had not yet done anything to give him excuse to find fault, so affording him little pleasure.)
In response I got a big pile of rage. The gist of same being that I needed to learn humility and take criticism.
One tiny problem: I cannot take criticism where none has been given.
Wednesday, 23 January 2013
Customising the NewForm.aspx of a List using Server Side Code
Edit - I am getting some feedback from other devs the sum of which is "forget this and do it via jQuery instead". It is worth noting that this is NOT the recommended way of customising a NewForm or EditForm aspx page, so proceed at your own risk. JQuery is downloadable for free at jquery.com. When I figure out how to use it I will post a follow up!
I've been meaning to write this for ages. The first thing I should mention is that you're not meant to do this. Believe me I have fought the Sharepoint markup generator every step of the way on this one. It wants you to use JQuery and the ECMA client script. But god JQuery is a pain and I don't know it. And it's hard to read or pick up, and dammit I want to use C flippin sharp and nothing and no-one is going to stop me.
OK. As we say in Ireland when someone asks for directions: "If I were you, I wouldn't start from here." But if you DO start from here...
Firstly, you aren't allowed to run code on aspx pages that run in SharePoint. So you have to go into the web config, go to the <PageParserPaths> section and enter something like the following:
<PageParserPath VirtualPath="http://myserver/mySiteColl/MyList/MyForm.aspx" CompilationMode="Always" AllowServerSideScript="true" />
More info on Page Parser Paths here...
OK run an iisreset and that will allow you to customise the NewForm.aspx. Microsoft do not recommend fiddling with the form itself as if you break it, you break the list. So fire up SharePoint Designer to create a new page.
More info on customising the page here
Now I'm aware that there's this nifty plugin called InfoPath which will do the forms for you. Do we have that in my workplace? Not a chance. Not on the cards until we upgrade. So just in case you ask me "what about infopath", that's the answer.
Right. I do this in SharePoint designer. Now that the MyForm.aspx has been created, click the "Code" tab. But before we continue it is important to note the following:
None of the standard SharePoint OK or Cancel buttons is customisable.
Trust me. I tried. Your best bet is hiding and creating a custom HTML command button of your own. Then you can have runat=server, onclick = "DoTheWork" etc. Then, of course, you have to create a script to do the work :)
So, right up the top of your HTML, put up the following. Note that
(1) when getting the list name, I am using the request parameter and ascertaining the position of the string "MyForm". This means I can re-use the code in other lists if I want.
(2) The ContentPlaceHolder bit of code is how I get to "talk" to the sharepoint form controls through the code. Trying to figure out where the controls are and what they're called and how to get the damn data out of them is like breaking into Fort Knox. So if I want to save back to the list (remember, there is no way to get to the save routine via the OK button BECAUSE I HAVE TRIED)
I need to go right into the markup, grab the guid of the data form web part and then furthermore pull out the name from the the properties tab on the left in SP Designer (it's always ff_number) and then get the value. And don't talk to me about radio buttons. I can't get it to work with radio buttons.
I've done a uservalue control because it's the trickiest.
(3) The ID at the end of the request string will always be the ID on the list item you're looking for, so I used that to retrieve the item.
(4) All saving of items is done through the SPListItem object and the Update() method
(5) I don't have a sharepoint environment and compiler with me right now, so chances are there are a couple of errors in the script, but nothing major. Code below the jump - enjoy!
I've been meaning to write this for ages. The first thing I should mention is that you're not meant to do this. Believe me I have fought the Sharepoint markup generator every step of the way on this one. It wants you to use JQuery and the ECMA client script. But god JQuery is a pain and I don't know it. And it's hard to read or pick up, and dammit I want to use C flippin sharp and nothing and no-one is going to stop me.
OK. As we say in Ireland when someone asks for directions: "If I were you, I wouldn't start from here." But if you DO start from here...
Firstly, you aren't allowed to run code on aspx pages that run in SharePoint. So you have to go into the web config, go to the <PageParserPaths> section and enter something like the following:
<PageParserPath VirtualPath="http://myserver/mySiteColl/MyList/MyForm.aspx" CompilationMode="Always" AllowServerSideScript="true" />
More info on Page Parser Paths here...
OK run an iisreset and that will allow you to customise the NewForm.aspx. Microsoft do not recommend fiddling with the form itself as if you break it, you break the list. So fire up SharePoint Designer to create a new page.
More info on customising the page here
Now I'm aware that there's this nifty plugin called InfoPath which will do the forms for you. Do we have that in my workplace? Not a chance. Not on the cards until we upgrade. So just in case you ask me "what about infopath", that's the answer.
Right. I do this in SharePoint designer. Now that the MyForm.aspx has been created, click the "Code" tab. But before we continue it is important to note the following:
None of the standard SharePoint OK or Cancel buttons is customisable.
Trust me. I tried. Your best bet is hiding and creating a custom HTML command button of your own. Then you can have runat=server, onclick = "DoTheWork" etc. Then, of course, you have to create a script to do the work :)
So, right up the top of your HTML, put up the following. Note that
(1) when getting the list name, I am using the request parameter and ascertaining the position of the string "MyForm". This means I can re-use the code in other lists if I want.
(2) The ContentPlaceHolder bit of code is how I get to "talk" to the sharepoint form controls through the code. Trying to figure out where the controls are and what they're called and how to get the damn data out of them is like breaking into Fort Knox. So if I want to save back to the list (remember, there is no way to get to the save routine via the OK button BECAUSE I HAVE TRIED)
I need to go right into the markup, grab the guid of the data form web part and then furthermore pull out the name from the the properties tab on the left in SP Designer (it's always ff_number) and then get the value. And don't talk to me about radio buttons. I can't get it to work with radio buttons.
I've done a uservalue control because it's the trickiest.
(3) The ID at the end of the request string will always be the ID on the list item you're looking for, so I used that to retrieve the item.
(4) All saving of items is done through the SPListItem object and the Update() method
(5) I don't have a sharepoint environment and compiler with me right now, so chances are there are a couple of errors in the script, but nothing major. Code below the jump - enjoy!
Friday, 19 October 2012
I Deleted My Last Post About Dynamically Rendering Controls From a SharePoint List
because I was stupid enough to forget that it is impossible, without a lot of fuss and bother, to add event handing to controls which are only rendered at runtime. I'll just take my dunce hat and sit in the corner.
It was not a total waste of time, however, as I was able to use the looping code and Response.Write to write the markup to a text file and then stick it back into the ASPX page. But as they say when looking for directions in Ireland, my home country, "well I wouldn't start from here if I were you."
Sigh. The loop DOES work well when submitting to list and that's coming up shortly.
It was not a total waste of time, however, as I was able to use the looping code and Response.Write to write the markup to a text file and then stick it back into the ASPX page. But as they say when looking for directions in Ireland, my home country, "well I wouldn't start from here if I were you."
Sigh. The loop DOES work well when submitting to list and that's coming up shortly.
Monday, 1 October 2012
Oi! My Content Editor Web Part is Borked!
Had an issue today where I got an email saying "ever had this error before?" It was a Content Editor Web Part (those thingies that allow you to add fancy text to a SharePoint page via a web part) that was causing the browser to throw errors. The browser was apparently failing to recognise the background colour or some other bit of javascript. Also the context menu that dropped down from the "edit" command on the web part - viewable when you select "Edit Page" - was not dropping down. The whole thing seemed borked.
I tried adding in a few other Content Editor Web Parts and for the most part they broke in the same way. I closed them using the Web Parts Maintenance Page (which is like the regular page only you add ?contents=1 to the URL) and then had a look at the original web part. Clicking Source Text Editor, I noticed the following.
<DIV blah blah blah piles of attributes that looked as if they'd been lifted out of MS Word etc etc>
<p>Text</p>
</DIV>
Since the DIV contained no text or formatting, I deleted it and refreshed the web part. Hey presto, the borkedness was fixed. The tags were interfering with the HTML. Interesting indeed if this is being caused by SharePoint's very own Rich Text Editor!
I tried adding in a few other Content Editor Web Parts and for the most part they broke in the same way. I closed them using the Web Parts Maintenance Page (which is like the regular page only you add ?contents=1 to the URL) and then had a look at the original web part. Clicking Source Text Editor, I noticed the following.
<DIV blah blah blah piles of attributes that looked as if they'd been lifted out of MS Word etc etc>
<p>Text</p>
</DIV>
Since the DIV contained no text or formatting, I deleted it and refreshed the web part. Hey presto, the borkedness was fixed. The tags were interfering with the HTML. Interesting indeed if this is being caused by SharePoint's very own Rich Text Editor!
Thursday, 27 September 2012
Help! I can't create a Site Collection without getting "Access Denied" errors!
Another one of those problems which is a short post but which took me hours upon hours to fix. The error is as follows:
You log into SharePoint Central Admin with the intent of creating a site collection. You go to Applications and do the necessary, ensuring it is added to the correct Web Application yada yada yada. And then you go to the new link for your site collection - because you want to create some sites in it, naturally enough.
Access Denied Error!
Butbutbut - I'm the Farm Administrator! How can this be? How can I not see my own damn site collections?
By the time I had found the solution, via the good offices of google, I was quite ready to be the Funny Farm Administrator. But never mind. I know now what it is.
1. Central Admin. Attempt to access SharedServices1 page. Chances are you will get an Access Denied error
2. Application Management - Policy for Web Application. Is your farm account listed there with Full Control? If not, add it for the relevant Web Application (you'll see the web app filter in the view dropdown on the top right)
3. Still having problem? Go to Operations - Service Accounts. BE VERY CAREFUL ABOUT DOING THIS ON A PRODUCTION SERVER, HAVE A BACKUP FIRST:
Click "Web application pool" option button
Select Web Service - Winds SharePoint Services Web Application
Select Application Pool - usually only Sharepoint - 80 available. That's fine.
Select "Configurable" radio button and enter farm admin account user and password
Click OK
A lot of the links in SharedServices will still be broken but you will be able to view SharedServices1 and more importantly your site collections!
You log into SharePoint Central Admin with the intent of creating a site collection. You go to Applications and do the necessary, ensuring it is added to the correct Web Application yada yada yada. And then you go to the new link for your site collection - because you want to create some sites in it, naturally enough.
Access Denied Error!
Butbutbut - I'm the Farm Administrator! How can this be? How can I not see my own damn site collections?
By the time I had found the solution, via the good offices of google, I was quite ready to be the Funny Farm Administrator. But never mind. I know now what it is.
1. Central Admin. Attempt to access SharedServices1 page. Chances are you will get an Access Denied error
2. Application Management - Policy for Web Application. Is your farm account listed there with Full Control? If not, add it for the relevant Web Application (you'll see the web app filter in the view dropdown on the top right)
3. Still having problem? Go to Operations - Service Accounts. BE VERY CAREFUL ABOUT DOING THIS ON A PRODUCTION SERVER, HAVE A BACKUP FIRST:
Click "Web application pool" option button
Select Web Service - Winds SharePoint Services Web Application
Select Application Pool - usually only Sharepoint - 80 available. That's fine.
Select "Configurable" radio button and enter farm admin account user and password
Click OK
A lot of the links in SharedServices will still be broken but you will be able to view SharedServices1 and more importantly your site collections!
Monday, 13 August 2012
Impersonating a SharePoint search
Here is code which will run a SharePoint search and return the results in a StreamWriter object, which can be outputted to a file. Hook it up to a console app and off you go!
Note
- you must specify a row limit or it will only return the first 50 results
- user under which it runs will need to have read access to the content database containing the Central Administration Shared Services. This is usually called SharedServices1_DB or some such variant. Error logs will indicate very clearly if that error occurs so make sure to have good error handling
- Microsoft.Office.Server.Search.Query is another one of those annoying DLLs that is impossible to find. It's not one of the SharePoint dlls - you might find it in the ISAPI folder or otherwise in the list of other .NET dlls.
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search;
public class SearchResults
{
public string errorArgs;
public SearchResults()
{
//constructor don't do nuthin'
errorArgs = string.Empty;
}
protected DataTable ReturnSearchResults(string searchCriterion, string siteUrl)
{
DataTable dt = new DataTable();
try
{
//Available fields: Title,FileExtension,ContentType,Created,LastModifiedTime,IsDocument,owsStartDate,Path
searchCriterion = searchCriterion.Replace(" ", "+");
string fullTextSQL = "SELECT Title, FileExtension, ContentClass, ContentType, Created, URL FROM SCOPE() ";
fullTextSQL = fullTextSQL + "WHERE CONTAINS ('" + searchCriterion + "') ";
//do whole site for the moment
using (SPSite site = new SPSite(siteUrl))
{
errorArgs = "accessing office search component";
Microsoft.Office.Server.Search.Query.FullTextSqlQuery search = newMicrosoft.Office.Server.Search.Query.FullTextSqlQuery(site);
Microsoft.Office.Server.Search.Query.ResultTableCollection results;
//configure the query - limit it to 50 for the moment
errorArgs = "preparing search";
search.QueryText = fullTextSQL;
search.ResultTypes = Microsoft.Office.Server.Search.Query.ResultType.RelevantResults;
search.RowLimit = 1000;
search.TrimDuplicates = true;
results = search.Execute();
errorArgs = "search successful";
if (results.Count > 0)
{
using (Microsoft.Office.Server.Search.Query.ResultTable relevantResults = results[Microsoft.Office.Server.Search.Query.ResultType.RelevantResults])
{ dt.Load(relevantResults, LoadOption.OverwriteChanges); }
}
return dt;
}
}
catch (Exception ex)
{
//handle error here
return null;
}
finally
{
}
}
public StreamWriter ReturnResultsInStream(string searchCriterion, string siteUrl,FileStream fs)
{
try
{
StreamWriter stream = new StreamWriter(fs);
DataTable results = this.ReturnSearchResults(searchCriterion, siteUrl);
foreach (DataRow dr in results.Rows)
{
foreach (object field in dr.ItemArray)
{
if (field is string)
{
stream.Write(field.ToString());
stream.Write(";");
}
}
stream.WriteLine();
}
return stream;
}
catch(Exception ex)
{
//HANDLE YOUR ERROR HERE
return null;
}
}
}
Subscribe to:
Posts (Atom)