Monday, October 27, 2008

Application Pages in SharePoint

Application pages allow you to create custom screens in SharePoint and make them available at any site level, similar to the accessdenied.aspx and error.aspx that come out of the box with SharePoint. To create an application page, follow the article by Ted Pattison to get the minimal code that is needed in an application page.


Once you have the page built you place it in the 12 hive in the Layouts folder. To access your cuastom page you then navigate to a site (i.e. http://sharepointserver/site/_layouts/nameofcustompage.aspx. This will display your custom page. You can access it at any level of your site.


Application pages provide an excellent way to add custom code to your site and can be debugged by attaching to the W3WP process on the SharePoint server that matches the AppId of the site you want.

Labels: , ,

Tuesday, October 21, 2008

The Network Path was not found. in SharePoint Search

Today I was working with Search Server Express 2008 to index a folder share on our network. I set up the share and pointed it to the correct path. I then set the content source to run a full crawl and monitored the results of the crawl. After 7 seconds it had finished the crawl and I knew that that was a little too soon considering there were a few items in that folder. So I took a look at the crawl logs and saw that there was an error that said 'The Network Path was not found.' So I clicked on the link that is above the error and was able to open the path just fine.


I then thought about the account that the crawl was running as and the account that I am using when clicking on the link. So I tried indexing a folder share that the content crawling account did have access to and it was able to index the content without any problems.


Something to keep in mind when using the SharePoint search or Search Server 2008: make sure that your content crawl access account has access to the content you want to crawl because the error log will not report a useful error. If you try to look into the SharePoint 12 hive logs you will not see any errors in there. So if you run across 'The Network Path was not found' error check the permissions on the share to make sure your index account has access to it.

Labels: , ,

Thursday, October 16, 2008

Minimal Master Page

One of the things I frequently find myself doing is creating a master page for some custom layout. There are many controls that need to be on a master page or errors will be thrown by SharePoint. To make sure you have all the controls the first time, Microsoft created an article with the code needed to create a minimal master page.


I use this article all the time and thought other people would like to have a place to go instead of trying to figure out what controls are needed.

Labels: , ,

Wednesday, October 15, 2008

User Information List

The other day I was trying to create a list event receiver that would fire when a user was added to SharePoint. I did some research and found the User Information List, which is the list that contains all the users in SharePoint. You access this list when you click on the People and Groups link. I built the event receiver and attached it to the list and added some users, only to find out that the event was not firing.

I did some debugging and the code was working fine, so I decided to attach the event receiver to another custom list and added some items to that. The event receiver fired perfectly. I used the U2U CAML Query Builder to run some queries against the User Information List and was able to get back the type of data I wanted in my event receiver.

I tried to do some more research and could not find anyone else out there that was trying to do the same thing as me and so I came to the conclusion that there is some special setting on the User Information List that does not allow the events to fire. I will keep this on the list of items to watch out for and if anyone has any ideas feel free to let me know.

Labels:

Sunday, October 12, 2008

Remote Debugging Custom Code in SharePoint

The other day I ran into a situation where I needed to debug an assembly that I had placed in the GAC of the SharePoint server and did not have Visual Studio installed on the SharePoint machine. I knew there were ways to attach to a process so I did a search to see if there was a way I could remotely attach to the process from Visual Studio running on my machine.


The process is pretty easy:


1. Locate the msvsmon.exe file located in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Remote Debugger\x86. This file is located on the machine that has Visual Studio installed on it.


2. Copy this file to the machine that you want to debug the code on (the SharePoint machine).


3. Run the exe. This will create a small window on the server that says the remote debugging server is waiting.


4. In Visual Studio on your machine, select Tools -> Attach to Process.


5. Get the process ID for the SharePoint web app you want to debug. You can do this by running the iisapp command from the SharePoint server command line. This will list the AppIDs and the name of the Web applications running on your server.


6. In the Attach to Process dialog, in the Qualifier drop down select the browse button and browse for the SharePoint machine.


7. Once you have the machine, select the process name (w3wp.exe) and the ID that matches the Web app that you want to debug.


8. Click attach and start debugging.


This works fairly well. Some things you are trying to do will not work all that well with the remote debugging, such as updating a list item, but it will allow you to step through your code to figure out why something might not be working.

Labels:

Wednesday, October 8, 2008

Accessing Attachments for an SPListItem

Today I was working on a project that needed some custom screens in SharePoint and manipulated a SharePoint list. I finished most of the screens to manipulate the list item itself and was about to move onto adding attachments to the list item. I began by try to iterate through each file in the SPAttachmentCollection that you can acess via the list item object. When I ran the page I kept getting an error that said it could not cast from string to SPFile object. I started commenting out lines to figure out where it was trying to cast a string into an SPFile because I did not do that explicitly in any of my code.



I finally got to the line that said For Each attachment As SPFile in AttachmentCollection. This was the line that was throwing the error. So I did some searching online and found that the SPAttachmentCollection only returns a string and not the actual attachments. The only way to get to the attachments is to navigate into the folder that holds the attachments.



The folder is located at Lists/{ListName}/Attachments/{ListItemID}. You can get to this programmatically in the following manner:



Dim folder As SPFolder = site.Folders("Lists").SubFolders(list.Title).SubFolders("Attachments").SubFolders(listItem.ID.ToString)



Once you have that folder you can iterate through each SPFile (that represents an attachment) and get back information such as the Url or the Name of the file. I used this syntax to render out the collection of attachments so the user could see them and open them. After figuring out this snag I was able to easily add attachments to a list item and then view the attachments for that list item.



As a side note, some people might be asking why I did not use the built-in New and Edit screens. I needed to be able to modify columns that were visible to certain groups of people. People that needed to review items had additional columns that they could fill out and those people that originally fill out the form should not see those columns. I tried accomplishing this in SharePoint Designer by customizing the New Form but I found that the attachment link no longer worked; it was throwing a Javascript error after I customized it, so I decided to create a custom screen that would allow me to run some security validations and redirect users to the proper form based on their group memberships.

Labels: , ,

Thursday, October 2, 2008

SPGridView and control with specified ID could not be found error

Today I was working on creating some custom SharePoint application pages that utilized the SPGridView to display data from a list I had in SharePoint. I used Powlo's blog post on SPGridViews as a starting point. I set up my GridView and attached it to my data source (SharePoint List) and was excited to see how easy it was to bind the data.


The next thing I wanted to do was filter the data in the SPGridView to only display items created by the current user. I found this post that explained how to build a select command using CAML that would filter down the data. This is exactly what I wanted.


So the next logical step in my journey was to allow the user to filter on different criteria and then filter the SPGridView and then re-display the data. I added a button to cause the page to post back and refresh the data in the GridView. I tested it out and my filter worked fine, THE FIRST TIME. The minute I went to click on the button after the initial post back, I got an ugly error -


"A control with the specified ID 'SubmittedListMenu' could not be found within the scope of the current naming container. ..." and it continued into the method calls.


I looked on the net for anything that looked similar to this error and found one post about issues with post backs and the SPGridView control. This did not help me out, but it got me thinking that the issue was probably in the fact taht data rows were available before that postback that would not be after the postback. So I went into the properties for the SPGridView and set the EnableViewState to false. I then went back to the page and tried it. This time since I was not tryint to keep track of the child controls in the GridView, I could keep hitting the submit button without any problems. As long as you don't need the ViewState enabled on that control, this is an easy solution to a problem that was not so easy to figure out.


Chalk up another afternoon to SharePoint debugging.

Labels: , ,