Archive for the ‘Development’ Category

You know that there is an ASP.Net Configuration section in VS 2005 or 2008  Website menu. By clicking this, an ASP.NET web site administration tool opens and you can configure your providers and create users and roles. In the development environment it’s easy to use because you simply click on Website and Asp.Net Configuration.

BUT in the production server you may not want to install visual studio . so how can you use this tool ?

  1. In the server where you have VS 2005 installed, from VS create a new simple web site which you want to Administer. It can be a simple web site, the main idea is that we want to use WSAT to create user in the production server.
  2. I am using File System to create the website and have called it CreateSQLUsers . ( Location : C:\Inetpub\wwwroot\CreateSQLUsers )
  3. Click on the ASP.NET Configuration button in your Solution Explorer’s toolbox or Website menu.
    We will need the URL soon (in my case, it is… http://localhost:1347/asp.netwebadminfiles/default.aspx?applicationPhysicalPath=C:\Inetpub\wwwroot\CreateSQLUsers\&applicationUrl=/CreateSQLUsers)
  4. Copy the contents of created web site to a folder in production server. ( I used the same path both in development and production server as C:\Inetpub\wwwroot\CreateSQLUser )
  5. Now, go to your production server, Open IIS console.
  6. Right Click on Web Sites -> New -> Web Site . Create a new web site and point it to the location where your website files is copied. ( C:\Inetpub\wwwroot\CreateSQLUser ) in my case site name is : SimpleSite on port : 200
  7. Right-Click on your Web Site and New -> Virtual Directory, Click on Next, give an Alias (WebSiteAdmin) and click Next
    In the Directory text box browse to… “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles” and click Next, Next and Finish.
  8. Right-click on WebSiteAdmin and hit properties, Click on the “ASP.NET” Tab and check ASP.NET Version as 2.0.50727 and click Ok.
  9. Right-Click on WebSiteAdmin and hit properties. In the “Virtual Directory” Tab if in the “Application name” section there’s no application, Click Create.
  10. Open an instance of Internet Explorer and in the Address Bar Paste the URL from the development environment… and do some changes like this :
    http://SimpleSite:200/WebSiteAdmin/default.aspx?applicationPhysicalPath=C:\Inetpub\wwwroot\CreateSQLUsers\&applicationUrl=/CreateSQLUsers
  11. You should be able to see ASP.NET Web Site Administration Tool (You can bookmark this so that you don’t have to remember that string)

As you can see, we have changed the site name and port number and changed asp.netwebadminfiles to our application WebSiteAdmin.

***

NOTE :

If you encounter this error :

An error was encountered. Please return to the previous page and try again. “

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: site at System.Web.Configuration.WebConfigurationHost.InitForConfiguration….

Here is reason and how to resolve :

The page responsible for opening the web.config file of the ASP.NET web application you specify is located in your ASP.NETWebAdminFiles folder, i.e. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles\. Then look for App_Code\WebAdminPage.cs. This much we know from the error.

On line 344 or 345 it is trying to open the configuration according to the application path you have specified in the querystring: return WebConfigurationManager.OpenMappedWebConfiguration(fileMap, path);

However, this OpenMappedWebConfiguration method has two more optional parameters, the first of which is SITE – the one we are getting the error about. So I added a third parameter to this function: return WebConfigurationManager.OpenMappedWebConfiguration(fileMap, path, “SimpleSite“); This works as a charm!!

***

The above solution was found here . also Thanks to Magician comment.

Advertisements
This is the code you can use :


    private static void ClearList(SPList listToClear)

    {

        int initialItemCount = listToClear.Items.Count;

         for (int counter = 0; counter < initialItemCount; counter++)

        {

            // Always delete the list item at index 0

            SPListItem listItemToClear = listToClear.Items[0];

            listItemToClear.Delete();

        }

        listToClear.Update();

    }

I know it is simple job for a developer but someone may need this one. With this Code you can copy all items with their attachments in a list to another site or web.  This is the code :


    protected void CopyList(SPList _sourceList, SPList _destList)

    {

        foreach (SPListItem item in _sourceList.Items)

        {

            SPListItem newDestItem = _destList.Items.Add();

            foreach (SPField field in _sourceList.Fields)

            {

                if (!field.ReadOnlyField && field.Type != SPFieldType.Attachments)

                {

                    newDestItem[field.InternalName] = item[field.InternalName];

                }

            }

            newDestItem.Update();

            if (item.Attachments.Count > 0)

            {

                SPFolder sourceItemAttachFolder = item.Web.Folders["Lists"].SubFolders[item.ParentList.Title]

              .SubFolders["Attachments"].SubFolders[item.ID.ToString()];

                foreach (SPFile file in sourceItemAttachFolder.Files)

                {

                    byte[] binFile = file.OpenBinary();

                    newDestItem.Attachments.AddNow(file.Name, binFile);

                }

            }

        }

        destList.Update();

    }

It’s simple and with a quick search you will find it . 



SPWeb web;

web = SPContext.Current.Web;

String webUrl = web.Url; // example : http://SiteCollection/SubSite

String webRelUrl = web. ServerRelativeUrl; // example : /SubSite

 
 

 
 

 

Sometimes when you want to deploy your solution or custom web part on the server , However everything is OK and the code is built successfully but you encounter this error.

Object reference not set to an instance of an object

It may be because of the existing deployed solutions. Mark Arend has written a good post about workaround this problem.

it is :

  1. Save & Close Visual Studio
  2. Retract & Remove deployed solutions. It mean the solutions that you want to deploy again. The quickest way to uninstall is to open a command window to the directory of your Project\bin\debug folder and enter setup /uninstall.  This runs faster than the solution management page in central administration Operation tab.  (that one’s performed by a timer job, which has a “now” setting but still seems to take a minute to launch itself).
  3. iisreset /noforce
  4. Open Visual Studio and deploy again .

Today I was working on my custom web part. One of my questions was : after pressing submit button how to update a people and group field in the destination list.

I tested many ways and they gives me this error :

Invalid data has been used to update the list item. The field you are trying to update may be read only.

after searching and googling for some hours I found it in my way , here it is :


SPListItemCollection itemCollection;
SPListItem item;
PeopleEditor people;
PickerEntity picker;
SPUserInfo userInf;
SPUser user;

itemCollection = web.Lists["ListName"].Items;
item = itemCollection.Add();

// I arranged my controls in a table
// here is my one of people editor controls
// So after pressing submit, the account of this control should set
// new Item "Assigned To" field in the related list.
people = (PeopleEditor)Tbl.Rows[0].Cells[0].Controls[0];  
picker = (PickerEntity)people.ResolvedEntities[0];
userInf = new SPUserInfo();
user = web.EnsureUser(picker.Key);
item["Assigned To"] = user;
item.Update();

with this kind of coding my list start updating correctly 🙂 ! hope this help you …

Also here is some related links that helped me :

I was trying to create a method to delete all items in a sharepoint list .

here is the code , It’s simple but a question remained for me at the end :


        public void DeleteAllRecords()
        {
            SPList list_a = web.Lists["UserDocs"];
            SPListItemCollection itemsCol = list_a.Items;
            if (itemsCol.Count > 0)
                for (int i = itemsCol.Count-1; i > -1; i--)
                {
                    itemsCol.Delete(i);
                }
        }

I don’t know why the “for” loop does not work when I used it in a incremental way .  I mean that I found that I should use it in a decremental way !! I’m trying to find the answer and if so I’ll post it soon.