Friday, October 28, 2011

Auto-Generating SharePoint Filenames for InfoPath 2010 on Submit

SharePoint 2010 InfoPath - Auto Generating Filename on Submit Button

1. Create a filename field

Create a strFilename field (Text).  Do not set any conditional formatting, rules, or data validation on this.  Add it to the canvas for now and make it read-only (Fig 1).  This will be visible for testing purposes only. You can hide this if you need to But I left it on my form as I thought it was useful.


2. Create Submit Data Connection

Create a SUBMIT data connection in your form template that connects to the Form Library where the form resides.  Put in the URL of your Form Library for the “Document Library” field, and choose the strFilename data element for “File name” field by using the fx button.  Check the box for “Allow overwrite if file exists”

In Infopath 2010 this is done by navigating to the "Data" tab, Clicking on "Data Connections", and clicking "Add" as in (Fig 2).

 (fig 2)

then click next and select to submit your data "To a document library on a sharepoint site". Click Next again and specify the location of your sharepoint library where you want the form to be submitted to and also the name of the field that you are going to use as the document title in that library. It is important to remember to use the fx button to select the field to avoid errors see (fig 3).

(fig 3)

3. Disable Save options in the browser

Next the toolbar options for saving in the browser form must be disabled to force the user to use the submit button that we will be creating.
To do this in infopath 2010 the following must be done.

Click on the "Developer" tab, Click on the "language" button and then navigate to the "web browser" settings see (fig 4).

 (fig 4)

Uncheck Save, and Save As (Fig 4) Submit should be grayed since it has not been configured.  If it has been configured, be sure it is unchecked.  Update should be unchecked by default, so leave it.

4.Create the custom Submit button Drag a button control to your canvas and double-click it to get to its properties.  Change the display name to “Submit” or whatever you prefer. We are now going to add some rules to the button which will perform the save logic for us.

Once the button is in position and modified to your liking, ensure the button is selected and click on the "Manage Rules" menu item from within the "Home" tab. This will invoke the rules editor for your submit button.

Click on the "New" drop down box in the rules editor and select "Action"
Change the name of this first rule to "Edit" Click on the hyperlink under the condition label to change the conditions under which this rule will run and set it as per (fig 5)

(fig 5)

Now we need to add the actions that will happen if the conditions in theis rule are satidfied (i.e teh filename is not blank)
Click on the "Add" dropdown next to the "run these actions" label and choose to submit the form as in (fig 6). using the data connection that we created earlier which points to our document library

(fig 6)

Finally for this rule we need to add one last action in the same way except this time we will choose the "Close the form" option. On completion of this rule it should look like (fig 7).

 (fig 7)

In essence this rule will only run if the strFilename variable already has a value (A filename) and simply resubmit the form to the same library with the same name and close the form returning the user to the library Hence the name Edit. If the filename has no value the rule will be skipped.
The second and final rule we need to create is the one that will run if no filename is present (i.e the form is being submitted for the first time)

Create a new rule for the submit button in the same way as we did above except this time the Rule should be named "Submit" and the condition should be set to only fire if the strFilename variable is empty as per (fig 8).

 (fig 8)

we now need to add three actions as we did above but there is an additional action to add first which sets the value of teh strFilename variable to a unique name. Under actions click add, and select "Set A fields value" When the dialog box appears use the fx button to buuild whatever unique filename you like, and apply it to the strFilename variable as per (fig 10).

 (fig 10)

After this action add a submit and close form action as we did for the first rule. The rule should look like that shown in (fig 11) when it is complete.

(fig 11)

In essence this rule will only fire when no filename exists, create a unique filename and submit it to the document library with that filename. Subsequent edits of this form will cause the first rule to fire and resubmit it with its original filename.

If you open or create a form in your SP library now, you will now no longer be able to save using the browser controls, and the submit button will be the only way to do this.

Moss 2007 :

Submit Options:

Create  textbox on Infopath form and name it to "strFilename".

 Place the Button on Inforpath path and name it "Submit", click the properties

Click "Submit" on Rules window

Friday, October 21, 2011

Copy a SharePoint List Programmatically

I've been working with the SharePoint API lately. One of my tasks is to copy SharePoint lists from one site to another site. This can be done in code as long as the two sites are on the same machine. It's OK if they are in different web applications, but it can't be from one server to another. If you need to copy from one server to another, you either need to do an export/import, or find a way to save the settings and import them on the new machine. I believe you can use the SharePoint Web Services to accomplish this, though. I haven't worked with that yet.

Anyway, it took a lot of digging around to figure out all the things I needed to copy lists, so I thought I'd post the code that is working for me. This post is only going to cover copying a basic list. I am planning on several more posts to handle document libraries, wikis, and other special cases. Each has their own challenges.

The requirements for me are to copy the bulk of the lists of a large SharePoint site to a new site with a different structure. Therefore, content and deploy is not going to work. Also, they are redesiging the pages, so they really only wanted the lists. What I'm doing is creating a new list on the new site and copying all the properties, so any metadata, such as who last modified the list, is unfortunately lost.

Here's the code:

// Instantiate web instances
SPSite sourceSite = new SPSite(@"http://SharePointServer:31001");

SPWeb sourceWeb = sourceSite.RootWeb;
SPSite destSite = new SPSite(@"http://SharePointServer:31002");
SPWeb destWeb = destSite.RootWeb;

// Get a reference to the source list
SPList sourceList = sourceWeb.GetList("/Lists/Announcements");

// if the list exists on the destination site, delete it

   SPList temp = destWeb.Lists[sourceList.Title];
catch { }

// create new list on the destination web with same properties
Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description,

SPList destList = destWeb.Lists[newListID];

// copy items
foreach (SPListItem item in announcements.Items)
   SPListItem newDestItem = destList.Items.Add();
   foreach (SPField field in sourceList.Fields)
      if (!field.ReadOnlyField)
      newDestItem[field.Id] = item[field.Id];

// set quicklaunch settings
destList.OnQuickLaunch = sourceList.OnQuickLaunch;

Thursday, October 20, 2011

Adding, Deleting, copying and downloading attachments in SPList using C#

using (SPSite oSPsite = new SPSite("http://website/ url/"))
using (SPWeb oSPWeb = oSPsite.OpenWeb())
            oSPWeb.AllowUnsafeUpdates = true;

            // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];
            //Add a new item in the List
            SPListItem itemToAdd = list.Items.Add();
            itemToAdd["Title"] = "Test Title";
            itemToAdd["Description"] = "Test Description";

            // Get the Item ID
            listItemId = itemToAdd.ID;

            // Update the List item by ID
            SPListItem itemToUpdate = list.GetItemById(listItemId);
            itemToUpdate["Description"] = "Changed Description";

            // Delete List item
            // Delete List item
          for (int i = list.Items.Count - 1; i >= 0; i--)

            oSPWeb.AllowUnsafeUpdates = false;