Saturday, November 10, 2007

Here are some more SharePoint hacks (continuing on from my last post here)

First off some hacks for the SPGridView which is actually a pretty useful control, but fails to do a few things, like displaying attachment columns as-per SharePoint's default List views (with a little paper-clip symbol), and the second (and much more annoying) it doesn't use the current users regional settings / timezone for formatting DateTime values.

The hacks:
And finally support for a ListForm replacement web part - after trying to customize the ListForm to suit a customers requirements by creating a CustomListForm in SharePoint designer I gave up in disgust because it's just too limited in what it lets you achieve, and worst of all, it breaks attachments support!  So I wrote a small web part to replace it, which allows for fine-grained control of visibility and editability (if that's even a word) of each field, and as an added bonus it doesn't break the attachments functionality.  I also wanted to add additional links/buttons to the form which triggered a save including validation etc. - so I did a little hack to implement that as well.
I might have a few more to post before the end of the project - but it's almost wrapped up, so these may be the last for a while.

Hopefully someone out there finds them useful.
posted @ Friday, November 09, 2007 9:38:26 PM (New Zealand Daylight Time, UTC+13:00)    Comments [8] | Trackback | Tracked by:
"Sharepoint Hacks - field visibility" (Bitter Coder) [Trackback]
Sunday, November 11, 2007 9:17:23 PM (New Zealand Daylight Time, UTC+13:00)
Actually I saw these a few days ago and thought 'damn those will be useful' ... they went straight in my Google Notebook :)
Sunday, November 11, 2007 11:30:15 PM (New Zealand Daylight Time, UTC+13:00)
It's not pretty code sadly, but you might find it useful... heh
Monday, November 12, 2007 8:11:03 AM (New Zealand Daylight Time, UTC+13:00)
Pretty code? Hell, if you can make any code that talks to SharePoint 'pretty' I will buy you a very large crate of beer!

One small question ... threw your code into a project this morning to shortcut my way through another business requirement, and noticed 2 lines of code that wouldn't compile:

List fields = new List(fieldsToRender);
List buttons = new List();


I fixed them with:
List<FormComponent> fields = new List<FormComponent>((IEnumerable<FormComponent>)fieldsToRender);
List<FormComponent> buttons = new List<FormComponent>();

But... what 'List' were they meant to be pointing to?


Also as a note for anyone else who uses the code, it also requires another piece of code from Alex, CustomTable (http://wiki.bittercoder.com/CustomTables.ashx)

Monday, November 12, 2007 8:31:31 AM (New Zealand Daylight Time, UTC+13:00)
Ack, I'm guessing Screwturn wished away the angle brackets because it I didn't escape it properly :)

I've added in the obligatory <esc></esc> and now everythings back to way it should be.

I'd be interested to know how you get on with that code Casey... it was gutted from a component tailored to a clients needs, so I haven't given it a great deal of testing in it's current form.
Monday, November 12, 2007 11:28:46 AM (New Zealand Daylight Time, UTC+13:00)
Well - I'm busy hacking away at it at the moment ... the replacements I made to the generic list bit worked after a little tweaking ... then the first problem I hit was that it was intended as a base type rather than to be instantiated. OFC me being lazy, I just dropped it into my site.

Firstly, those 'yield' statements had to go ... :) Sorry a bit behind here ...

Secondly my comment above was slightly wrong (I picked the wrong type so it would compile but not run), correct version is :

List<FieldToRender> fields = new List<FieldToRender>((IEnumerable<FieldToRender>)fieldsToRender);


As such, it sort of works, but doesn't ... because it displays every damn field that SP knows about, and then it can't write back to them ... hence you commented TODO Add logic etc

Then I replaced the EnumerateFields method (for the time being) rather than derive. With a few alterations it seems to display the fields that can actually be edited, and lets me add things... more work to come, I'll feed back what I can ... this will be a great basis for the task I was given (volunteered for) on Friday!

protected virtual IEnumerable EnumerateFields(SPList list)
{
List<FieldToRender> newList = new List<FieldToRender>();
foreach (SPField field in list.Fields)
{
if (field.Type != SPFieldType.Counter && field.ReadOnlyField == false && field.Hidden == false && !IsInternalField(field))
{
bool isVisible = true;
bool isWriteable = true;
newList.Add(new FieldToRender(field, isVisible, isWriteable));
}
}
return newList;
}

protected string[] SPInternalFields = new string[] { "Author", "Editor", "Created", "Modified" };

protected virtual bool IsInternalField(SPField field)
{
string fieldName = field.InternalName;
foreach (string internalField in SPInternalFields)
{
if (fieldName == internalField) return (true);
}

return (false);

}
Monday, November 12, 2007 12:15:28 PM (New Zealand Daylight Time, UTC+13:00)
OK- small quirk ... It is sort of working ... in DisplayMode.New I can add items to my list, but attachments are failing ...

The list item was saved, but one or more attachments could not be saved.
Could not upload file C:\Documents and Settings\Administrator\Desktop\MsUnitTest.xml.


Actually it is lying, the attachment is saved, and linked, but the error displays. Go back to the main list view and the update is there, along with the file.

If you view the item with DisplayMode.Edit the attachment shows twice:

Title *
Attachments UnitTestTemplates.xml Delete
Attachments UnitTestTemplates.xml Delete


And if you do try to delete the attachment it removes it from the screen, but when you hit OK you get:

Failed to get value of the "Attachments" column from the "Attachments" field type control. See details in log. Exception message: Specified argument was out of the range of valid values..


If you have any ideas let me know, or if I get any bright spark of genius and fix it, I'll let you know ...
Sunday, November 18, 2007 7:06:01 PM (New Zealand Daylight Time, UTC+13:00)
Hi,

I managed to deploy and add the web part to SharePoint Custom List form. The web part displays input fields for all the list's fields. In your post you mentioned that you're using an XML configuration file, can you please post a sample configuration file? Furthermore, can you please provide more guidance about deploying this solution (web part and configs)...

Thanks for your reply. By the way... great work!!
LebStar
Monday, November 19, 2007 10:03:02 AM (New Zealand Daylight Time, UTC+13:00)
Hi LebStar,

I'll post more about this tomorrow when I have time - but here's the code I use for establishing which columns should be displayed at all:

private static bool IsHidden(SPField field)
{
return (field.Hidden || field.ReadOnlyField || field.Type == SPFieldType.Attachments);
}


foreach (SPField field in list.Fields)
{
if (!IsHidden(field))
{
if (!field.ShowInNewForm.HasValue || field.ShowInNewForm.Value)
{
// include this field on the new form
}
if (!field.ShowInEditForm.HasValue || field.ShowInEditForm.Value)
{
// include this field on the edit form
}
}
}

When I get a little more time I'll post some more code about how I had this web part auto-generate a default column configuration file for new/edit modes which could then be edited to change the order/visibile/editability etc. It's pretty trivial though.

Cheers,

- Alex
Comments are closed.
Search
FeedCount

Tags...
Who am I?
Alex Henderson
Alex Henderson
Auckland, New Zealand
Managing Director at Dev|Defined Limited

"Self Confessed Coding Junky for 15 years"
View Alex Henderson's profile on LinkedIn
 
Mobile: +64-21-402-969
Email: bittercoder 'at' gmail 'dot' com
MSN: bittercoder_nz@hotmail
Skype: alex.devdefined
Navigation